In shorts of Pset4 it was mentioned, that pointers are created in heap. Does it mean, that all arrays are stored in heap?
1 Answer
Arrays you declare as int array[42];
are stored on stack, like any other variable.
For arrays like int *array = (int*)malloc(42*sizeof(int));
, a pointer array
is stored on stack, the memory block of 42 ints is allocated on the heap. Don't confuse those two parts. While the stack part ends its life on exiting its scope (the block it was declared in), the heap part requires an explicit free
of some kind.
If, however, an array is part of a struct, it is stored as part of the struct, and has the same lifecycle as the struct.