32bit架构下各种指针大小的面试题
char str[] = " http://www.ibegroup.com/";
char *p = str ;
void Foo ( char str[100]){
}
void *p = malloc( 100 );
上述4种情况中的sizeof
str
,p
,str
,p
是多少反过来?
我已经在我的机器(似乎是64位)下测试了它,结果如下:
25 8 8 8
但还不明白原因。
char str[] = " http://www.ibegroup.com/";
char *p = str ;
void Foo ( char str[100]){
}
void *p = malloc( 100 );
What's the sizeof
str
,p
,str
,p
in the above 4 case in turn?
I've tested it under my machine(which seems to be 64bit) with these results:
25 8 8 8
But don't understand the reason yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
sizeof(char[])
返回字符串中的字节数,即strlen()+1
(对于填充整个数组的以 null 结尾的 C 字符串)。 数组不会衰减为sizeof
中的指针。str
是一个数组,字符串有 25 个字符加上一个空字节,因此sizeof(str)
应该是 26。您是否在该值中添加了空格?当然,指针的大小始终仅由机器体系结构决定,因此
p
的两个实例在 64 位体系结构上都是 8 字节,在 32 位体系结构上都是 4 字节。在函数参数中,数组 do 衰减为指针,因此您得到的结果与指针得到的结果相同。因此,以下定义是等效的:
sizeof(char[])
returns the number of bytes in the string, i.e.strlen()+1
for null-terminated C strings filling the entire array. Arrays don't decay to pointers insizeof
.str
is an array, and the string has 25 characters plus a null byte, sosizeof(str)
should be 26. Did you add a space to the value?The size of a pointer is of course always determined just by the machine architecture, so both instances of
p
are 8 bytes on 64-bit architectures and 4 bytes on 32-bit architectures.In function arguments, arrays do decay to pointers, so you're getting the same result that you get for a pointer. Therefore, the following definitions are equivalent:
第一个是内置数组的
sizeof
,它是元素的数量(24 + 字符串末尾的 null)。第二个是指针的
sizeof
,它是系统的本机字大小,在您的情况下为 64 位或 8 字节。第三个是指向数组第一个元素的指针的 sizeof ,该元素的大小与任何其他指针相同,即系统的本机字大小。为什么要使用指向数组第一个元素的指针?因为数组的大小信息在传递给函数时会丢失,并且会隐式转换为指向第一个元素的指针。
第四个是指针的
sizeof
,它与任何其他指针的大小相同。The first is the
sizeof
of an built-in array, which is the amount of elements (24 + null on the end of the string).The second is the
sizeof
of a pointer which is the native word size of your system, in your case 64 bit or 8 bytes.The third is the
sizeof
of a pointer to the first element of an array which has the same size as any other pointer, the native word size of your system. Why a pointer to the first element of an array? Because size information of an array goes lost when passed to a function and it gets implicitly converted to a pointer to the first element instead.The fourth is the
sizeof
of a pointer which has the same size as any other pointer.str
是一个 8 位字符的数组,包括 null 终止符。p
是一个指针,通常是机器本机字大小(32 位或 64 位)的大小。无论指针指向的内存大小如何,指针占用的大小都保持不变。
编辑
在 C++ 中,数组参数是通过引用传递的(内部是指针类型),这就是为什么
str
的第二个实例具有sizeof
8.str
is an array of 8-bit characters, including null terminator.p
is a pointer, which is typically the size of the machine's native word size (32 bit or 64 bit).The size taken up by a pointer stays constant, regardless of the size of the memory to which it points.
EDIT
In c++, arguments that are arrays are passed by reference (which internally is a pointer type), that's why the second instance of
str
hassizeof
8.在这种情况下,已知 的大小
为 25 (24+1),因为实际分配了那么多内存。
的情况下
在没有分配内存
in the cases the size of
is known to be 25 (24+1), because that much memory is actually allocated.
In the case of
no memory is allocated