C语言 指向结构的指针的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23761267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:06:15  来源:igfitidea点击:

Size of a pointer pointing to a structure

cpointersstructure

提问by c_b

I was trying to remember the basics of C programming, and regarding pointers to structures I was doing the following:

我试图记住 C 编程的基础知识,关于指向结构的指针,我正在执行以下操作:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main()
{
    struct MyStruct {
    int number;
    char *name;
    };

int i;

struct MyStruct *p_struct = (struct MyStruct *) malloc (sizeof(struct MyStruct)*3);
printf("sizeof(struct MyStruct) = %d\n", sizeof(struct MyStruct));

for (i=0; i<3;i++)
{
    (*p_struct).number = i;
    (*p_struct).name = "string";
    printf("(*p_struct).number = %d, (*p_struct).name = %s\n", (*p_struct).number (*p_struct).name);
    ++p_struct;
}

printf("sizeof(p_struct) = %d\n", sizeof(p_struct));
free (p_struct);
return 1;
}

My question is: I get 8 bytes as the size of the structure, which is okay as 4+4 = 8 bytes due to alignment/padding of the compiler, but why do I get 4 bytes from sizeof(p_struct)?

我的问题是:我得到 8 个字节作为结构的大小,由于编译器的对齐/填充,4+4 = 8 个字节是可以的,但是为什么我从 中得到 4 个字节sizeof(p_struct)

I was expecting to get 24 (8 bytes x 3), why is this so?

我期望得到 24(8 个字节 x 3),为什么会这样?

If this is correct, can I get the total allocated size of 24 bytes somehow?

如果这是正确的,我可以以某种方式获得 24 个字节的总分配大小吗?

Thanks.

谢谢。

回答by Some programmer dude

No, the size of the structure is eight bytes because you have two four-byte fields in it, try printing sizeof(int)and sizeof(char *)and see for yourself.

不,该结构的大小是 8 个字节,因为其中有两个 4 个字节的字段,请尝试打印sizeof(int)sizeof(char *)亲自查看。

When you do sizeofof a pointer, you always gets the size of the pointer and never what it points to. There is no way (in standard C) to get the size of memory you have allocated with malloc.

当你sizeof处理一个指针时,你总是得到指针的大小,而不是它指向什么。没有办法(在标准 C 中)获得您分配的内存大小malloc

Also note that you have undefined behaviorin your code, as you change the pointer p_structso it no longer points to what your malloccall returned. This leads to the undefined behavior when you try to freethat memory.

另请注意,您的代码中有未定义的行为,因为您更改了指针,p_struct因此它不再指向您的malloc调用返回的内容。当您尝试使用free该内存时,这会导致未定义的行为。

回答by Clinton Pierce

Pointers are always the same size on a system no matter what they're pointing to (int, char, struct, etc..); in your case the pointer size is 4 bytes.

指针在系统上总是相同的大小,无论它们指向什么(int、char、struct 等);在您的情况下,指针大小为 4 个字节。

回答by gnasher729

p_struct is a pointerto a struct. Pointers usually take either 4 or 8 bytes. If you wanted to know the size of the struct itself, you would use sizeof (*p_struct).

p_struct 是指向结构的指针。指针通常占用 4 或 8 个字节。如果您想知道结构本身的大小,您可以使用 sizeof (*p_struct)。

Note that your code is likely to crash, because you increased p_struct three times, and then call free () on the result, not on the original pointer that malloc returned. Much clearer and safer to write for example

请注意,您的代码很可能会崩溃,因为您将 p_struct 增加了 3 次,然后在结果上调用了 free(),而不是在 malloc 返回的原始指针上调用。例如,写起来更清晰、更安全

for (i=0; i<3;i++)
{
    p_struct [i]->number = i;
    p_struct [i]->name = "string";
    printf("(*p_struct).number = %d, (*p_struct).name = %s\n", p_struct [i]->number p_struct [i]->name)
}