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 int
s) 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.
Arrays are not pointers:
int arr[10]
:Amount of memory used is
sizeof(int)*10
bytesThe values of
arr
and&arr
are necessarily identicalarr
points to a valid memory address, but cannot be set to point to another memory addressint* ptr = malloc(sizeof(int)*10)
:Amount of memory used is
sizeof(int*) + sizeof(int)*10
bytesThe 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 willSize 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.
There is no pointer, just an array. Therefore the struct is of size sizeof( int[5] ) ( plus possible padding ).
The struct
node
and its membernumbers
share the address. If you have a variable of typenode
or a pointer to that variable, you can access its member.When you have a variable such as
int x;
space is set aside for the value. Whenever the identifierx
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 5int
s. When the identifierarr
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 meanx
contains the address of anint
... just that the compiler knows how to figure it out.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.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.