3

Let's say we have a struct Vector2i { int x = 0, int y = 0 };

And create a Pointer to it via Vector2i* pointer = new Vector2i; Where would int x and int y be stored? Heap or stack?

Are all members of an instance a Pointer is pointing to automatically on the heap because the vessel is? Are members of stack created vessels automatically on the stack? What about Pointers inside a class that was created on the stack?

Thanks in advance and feel free to point me to articles referring to this topic (My terminology is probably bad)

1

1 Answer 1

6

If the structure's memory is on the heap, so too are its members, since the struct and its members are not separate chunks of memory. In fact, the struct doesn't even exist to put it one way, only a contiguous region of memory which represents and stores the data fields of the structure. Of course if you have a pointer to a structure, then the pointer resides in its own separate chunk of memory, but the memory allocated for the struct will typically be one indivisible chunk of memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.