Assuming the definition of Stack
is like this:
typedef struct Stack{
int capacity;
int top;
int *array;
} Stack;
Then the amount of allocated space for *stack
should be sizeof(int) /* for capacity */ + sizeof(int) /* for top */ + *X* /* for *array */
(i.e- the sum of all the members' sizes of the struct).
On the other hand, amount of allocated space for array
will be capacity*sizeof(int)
.
The allocations of *stack
and *array
are done independently in your case - because when you are making allocation for *stack
, you are not constructing any array - you are just allocating for a pointer (along with other struct
members) - which can not only be used to create an array, but can also be used to point a single int
variable. After that allocation, you made an allocation for an array using the pointer you just created. The later allocation (*array
) might be made adjacent to your previous allocation (*stack
) - but it depends on the OS.
*X*
: The size of a pointer depends on the architecture of you PC (i.e- your processor, RAM capacity, OS version (Win/Linux/Mac,x86/x64)), compiler options, as well as what type of pointer it is (i.e- is it an int
pointer, or a double
pointer). (Reference 1,2,3; and an example)