Memory is either statically or dynamically allocated. Dynamic allocation is what you get when allocating memory at runtime, with for instance new
and malloc
. Static allocation is "the rest". Memory for class member variables are allocated with the class instance, so if it is statically allocated, the members end up in the same part of the memory, and if it is dynamically allocated, the members end up where the dynamic memory resides. This is also true for pointer member variables, but the actual memory it points at can be either dynamically (new
and malloc
) or statically allocated.
int i = 0;
int* pi = &i; // pi points at statically allocated memory
pi = new int(0); // pi points at dynamically allocated memory
So, if you have a static class instance, memory for it and its members is usually allocated in the code segment, but that is an implementation detail.
If the member is a pointer, which points at dynamically allocated memory, that memory will be where the used allocator decides. The "heap" is the most common implementation detail of dynamically allocated memory, which you normally get when using new
and malloc
, but a custom allocator can be used that controls memory elsewhere, even in the code segment.