How do arrays work inside a struct?

2019-07-23 09:09发布

If I have for example

typedef struct node
{
    int numbers[5];
} node;

Whenever I create an instance of such a struct there's gonna be allocation of memory in the stack for the array itself, (in our case 20 bytes for 5 ints(considering ints as 32 bits)), and numbers is gonna be a pointer to the first byte of that buffer. So, I thought that since inside an instance of node, there's gonna be a 20 bytes buffer(for the 5 ints) and a 4 bytes pointer(numbers), sizeof(node) should be 24 bytes. But when I actually print it out is says 20 bytes.
Why is this happening? Why is the pointer to the array not taken into account?
I shall be very grateful for any response.

5条回答
闹够了就滚
2楼-- · 2019-07-23 09:10

Arrays are not pointers:


  • int arr[10]:

    • Amount of memory used is sizeof(int)*10 bytes

    • The values of arr and &arr are necessarily identical

    • arr points to a valid memory address, but cannot be set to point to another memory address


  • int* ptr = malloc(sizeof(int)*10):

    • Amount of memory used is sizeof(int*) + sizeof(int)*10 bytes

    • The values of ptr and &ptr are not necessarily identical (in fact, they are mostly different)

    • ptr can be set to point to both valid and invalid memory addresses, as many times as you will

查看更多
劳资没心,怎么记你
3楼-- · 2019-07-23 09:15

Size of structure is always defined by the size of its members. So its really doesn't matter whether members are simply int, char, float or arrary or even structure itself.

查看更多
混吃等死
4楼-- · 2019-07-23 09:17

There is no pointer, just an array. Therefore the struct is of size sizeof( int[5] ) ( plus possible padding ).

The struct node and its member numbersshare the address. If you have a variable of type node or a pointer to that variable, you can access its member.

查看更多
仙女界的扛把子
5楼-- · 2019-07-23 09:20

When you have a variable such as int x; space is set aside for the value. Whenever the identifier x is used, the compiler generates code to access the data in that space in the appropriate manner... there's no need to store a pointer to it to do this (and if there were, wouldn't you need a pointer to that pointer? And a pointer to that? etc.).

When you have an array like int arr[5];, space is set aside the same way, but for 5 ints. When the identifier arr is used, the compiler generates code to access either the relevant array element or give the address of the array (depending on how it's used). The array is not a pointer, and doesn't contain one... but the compiler may use its address instead of its contents in some situations.

An array is said to decay to a pointer to its first element in many situations, but that just means that in those situations the identifier will give its address instead of its contents, much like when you use the address-of operator with a non-array variable. The fact that you can get the address of the int x with &x doesn't mean x contains the address of an int... just that the compiler knows how to figure it out.

查看更多
对你真心纯属浪费
6楼-- · 2019-07-23 09:32

Arrays don't work like that. They only allocate space for their elements, but not for a pointer. The "pointer" you are talking about (numbers) is just a placeholder for the address of the array's first element; think of it as a literal, instead of a variable. Therefore, you can not assign a value to it.

int myint;
numbers = &myint;

This won't work, since there is no memory where you could store &myint. numbers will just be converted to an address at compile time.

查看更多
登录 后发表回答