No, global data are not allocated on the stack. They are allocated statically and the memory is reserved at compile time.
One simple way to think about this is to consider threads. There is one stack per thread. But global data is shared between threads. So global data cannot be allocated on a stack.
Other types of global variable are on the heap.
Not so. Global data is never allocated on the heap. Heap allocation is performed dynamically at runtime.
Perhaps you have a pointer global variable. And you assign a dynamic array to that pointer. In that scenario the pointer is a global, but the array is a dynamic heap allocated object.
So perhaps you have code like this:
int *arr;
....
arr = calloc(N, sizeof(int));
In that scenario, arr
is a global object, but *arr
is heap allocated.