32bit架构下各种指针大小的面试题

发布于 2024-11-17 01:16:48 字数 335 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音栖息无 2024-11-24 01:16:48

sizeof(char[]) 返回字符串中的字节数,即 strlen()+1(对于填充整个数组的以 null 结尾的 C 字符串)。 数组不会衰减为 sizeof 中的指针。 str 是一个数组,字符串有 25 个字符加上一个空字节,因此 sizeof(str) 应该是 26。您是否在该值中添加了空格?

当然,指针的大小始终仅由机器体系结构决定,因此 p 的两个实例在 64 位体系结构上都是 8 字节,在 32 位体系结构上都是 4 字节。

在函数参数中,数组 do 衰减为指针,因此您得到的结果与指针得到的结果相同。因此,以下定义是等效的:

void foo(char s[42]) {};
void foo(char s[100]) {};
void foo(char* s) {};

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 in sizeof. str is an array, and the string has 25 characters plus a null byte, so sizeof(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:

void foo(char s[42]) {};
void foo(char s[100]) {};
void foo(char* s) {};
债姬 2024-11-24 01:16:48

第一个是内置数组的 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.

鲜血染红嫁衣 2024-11-24 01:16:48

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 has sizeof 8.

允世 2024-11-24 01:16:48

在这种情况下,已知 的大小

char str[] = “ http://www.ibegroup.com/”

为 25 (24+1),因为实际分配了那么多内存。

的情况下

void Foo ( char str[100]){

在没有分配内存

in the cases the size of

char str[] = “ http://www.ibegroup.com/”

is known to be 25 (24+1), because that much memory is actually allocated.

In the case of

void Foo ( char str[100]){

no memory is allocated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文