I tried to test the length of int
and short
type in my computer (X86_64), so I wrote two pieces of codes as bellow: short:
我尝试在我的计算机(X86_64)中测试int和short类型的长度,所以我写了两段代码:short:
short a;
scanf("%hd%hd",&a,&a+1);
printf("%hd",a+*(&a+1));
int:
int(警官):
int a;
scanf("%d%d",&a,&a+1);
printf("%d",a+*(&a+1));
Both work and output the right answer, but when I change to number 1
to 4
, the first one works well while the second one show a Segmentation fault
after input.
Some materials say that the lengths of short and int are all 16 bits in x86, I don't know the difference in x86_64, are they the same? In addition, what caused the Segmentation fault
?
工作和输出都是正确的答案,但是当我改变到数字1到4时,第一个工作很好,第二个在输入后显示一个分割错误。有些材料说,在x86中,短和整数的长度都是16位,我不知道x86_64的区别,它们是一样的吗?此外,是什么导致了分割错误?
PS:I use gcc -Wall -O2 -o filename -lm
to compile.
我使用gcc -Wall -O2 -o文件名-lm来编译。
3 个解决方案
#1
3
All x86 ABIs define sizeof(short) == 2
.
All x86 16-Bit ABIs define sizeof(int) == 2
, all other x86 ABIs define sizeof(int) == 4
. For standard 64Bit mode, every pointer is 64Bit == 8 Byte big, for 64Bit 4G-Address mode and for 32Bit flat memory mode (flat is standard), every pointer is 32Bit == 4 Byte, and for 16Bit mode it varies.
所有x86 ABIs定义sizeof(short) == 2。所有x86 16位ABIs定义sizeof(int) == 2,所有其他x86 ABIs定义sizeof(int) == 4。对于标准的64位模式,每个指针都是64位== 8字节的大,对于64位的4g地址模式和32位平的内存模式(单位是标准的),每个指针都是32位== 4字节,而对于16位模式则是不同的。
This example has Undefined Behavior, because you do not own a short
-object at &a+1
:
这个例子有未定义的行为,因为你没有一个短对象在和+1:
short a;
scanf("%hd%hd",&a,&a+1);
printf("%hd",a+*(&a+1));
Same for this one, just with int
:
对于这个也是一样,只是用int:
int a;
scanf("%d%d",&a,&a+1);
printf("%d",a+*(&a+1));
Undefined Behavior means anything may happen, even the proverbial nasal demons.
That includes it seems to work as well as reformatted your drive.
未定义的行为意味着任何可能发生的事情,甚至是众所周知的鼻魔。这包括它似乎工作和重新格式化你的驱动器。
#2
0
You can get the size of short
by:
你可以通过以下方式获得:
printf("size of short: %d\n", sizeof(short));
You can get the size of short
pointer, by:
你可以得到短指针的大小:
printf("size of pointr: %d\n", sizeof(short*));
I believe your other questions have been answered by Deduplicator.
我相信你的其他问题已经被de复印机解决了。
#3
0
I have known what's wrong with my code after reading your comments and answers.
I guessed the address of a variable is like that in assembly language, so if I get an address and add it with the length of a variable I can get the address of the following viable. I use &a+4
first to test if the length of int
is 4 BYTES
, but there was a Segmentation fault
while running, I changed 4
to 1
and get the correct value, I didn't understand why this worked? Now, I got it.
In the following code:
在阅读了您的评论和答案后,我已经知道我的代码有什么问题。我猜一个变量的地址就像汇编语言中那样,所以如果我得到一个地址并加上一个变量的长度,我就能得到下面可行的地址。我先用and +4来测试int的长度是否为4个字节,但是在运行的时候有一个分割错误,我将4改为1并得到正确的值,我不明白为什么会这样?现在,我明白了。在以下代码:
short a,b;
a = b = 1024;
cout << &a << endl << &b << endl;
cout << &a << endl << (&a) + 1;
An output of it is:
它的输出是:
0xbfad153c
0xbfad153e
0xbfad153c
0xbfad153e
And
和
int a,b;
a = b = 1024;
cout << &a << endl << &b << endl;
cout << &a << endl << (&a) + 1;
return 0;
An Output is:
一个输出是:
0xbfddb968
0xbfddb96c
0xbfddb968
0xbfddb96c
It shows that &a + 1
here plus the value of the size of int a
but not the literal const value
of 1
So I just want to use the feature of memory allocating to measure the size of int
and short
, not sizeof
function.
Thanks for your answers and comments, even though they are not what I want to get.
它显示了这里的&a + 1加上int a大小的值,而不是常量1的值,所以我想用内存分配的特性来测量int和short的大小,而不是sizeof函数。谢谢你的回答和评论,尽管它们不是我想要的。
#1
3
All x86 ABIs define sizeof(short) == 2
.
All x86 16-Bit ABIs define sizeof(int) == 2
, all other x86 ABIs define sizeof(int) == 4
. For standard 64Bit mode, every pointer is 64Bit == 8 Byte big, for 64Bit 4G-Address mode and for 32Bit flat memory mode (flat is standard), every pointer is 32Bit == 4 Byte, and for 16Bit mode it varies.
所有x86 ABIs定义sizeof(short) == 2。所有x86 16位ABIs定义sizeof(int) == 2,所有其他x86 ABIs定义sizeof(int) == 4。对于标准的64位模式,每个指针都是64位== 8字节的大,对于64位的4g地址模式和32位平的内存模式(单位是标准的),每个指针都是32位== 4字节,而对于16位模式则是不同的。
This example has Undefined Behavior, because you do not own a short
-object at &a+1
:
这个例子有未定义的行为,因为你没有一个短对象在和+1:
short a;
scanf("%hd%hd",&a,&a+1);
printf("%hd",a+*(&a+1));
Same for this one, just with int
:
对于这个也是一样,只是用int:
int a;
scanf("%d%d",&a,&a+1);
printf("%d",a+*(&a+1));
Undefined Behavior means anything may happen, even the proverbial nasal demons.
That includes it seems to work as well as reformatted your drive.
未定义的行为意味着任何可能发生的事情,甚至是众所周知的鼻魔。这包括它似乎工作和重新格式化你的驱动器。
#2
0
You can get the size of short
by:
你可以通过以下方式获得:
printf("size of short: %d\n", sizeof(short));
You can get the size of short
pointer, by:
你可以得到短指针的大小:
printf("size of pointr: %d\n", sizeof(short*));
I believe your other questions have been answered by Deduplicator.
我相信你的其他问题已经被de复印机解决了。
#3
0
I have known what's wrong with my code after reading your comments and answers.
I guessed the address of a variable is like that in assembly language, so if I get an address and add it with the length of a variable I can get the address of the following viable. I use &a+4
first to test if the length of int
is 4 BYTES
, but there was a Segmentation fault
while running, I changed 4
to 1
and get the correct value, I didn't understand why this worked? Now, I got it.
In the following code:
在阅读了您的评论和答案后,我已经知道我的代码有什么问题。我猜一个变量的地址就像汇编语言中那样,所以如果我得到一个地址并加上一个变量的长度,我就能得到下面可行的地址。我先用and +4来测试int的长度是否为4个字节,但是在运行的时候有一个分割错误,我将4改为1并得到正确的值,我不明白为什么会这样?现在,我明白了。在以下代码:
short a,b;
a = b = 1024;
cout << &a << endl << &b << endl;
cout << &a << endl << (&a) + 1;
An output of it is:
它的输出是:
0xbfad153c
0xbfad153e
0xbfad153c
0xbfad153e
And
和
int a,b;
a = b = 1024;
cout << &a << endl << &b << endl;
cout << &a << endl << (&a) + 1;
return 0;
An Output is:
一个输出是:
0xbfddb968
0xbfddb96c
0xbfddb968
0xbfddb96c
It shows that &a + 1
here plus the value of the size of int a
but not the literal const value
of 1
So I just want to use the feature of memory allocating to measure the size of int
and short
, not sizeof
function.
Thanks for your answers and comments, even though they are not what I want to get.
它显示了这里的&a + 1加上int a大小的值,而不是常量1的值,所以我想用内存分配的特性来测量int和short的大小,而不是sizeof函数。谢谢你的回答和评论,尽管它们不是我想要的。