Reputation: 19
So we have 2 main memories stack and heap. When I create an simple object of class(A obj) which is heap allocated and if we have data members which require heap allocation and stack allocation then what does actually happens? How much memory is allocated on heap while creating object and are stack allocated data members excluded from this heap allocation for object.
class A{
public:
int height;
int * weight;
A(){
height=0;
weight= new int(100);
}
};
int main(){
A *obj = new A(); // what's the memory allocation for object
return 0;
}
My assumption is when we create heap allocated object then data members which require stack allocation are stack allocated and not counted for object's memory allocation on heap and other data members which require heap allocation are heap allocated and are consider for objects memory allocation. So in above allocation we only need 4bytes of heap memory as only have single data member which is pointer.
Upvotes: 0
Views: 153
Reputation: 1144
That is a really good question. The initialization in C++ is the most complicated topic, so do not hesitate to ask questions. Even experienced developers may misunderstand some concepts.
There are no heap data members or stack data members. All the data members inside the class are allocated one by one close to each other's in the order as they are declared in the class.
What you are missing are the pointers. Pointer is similar to the unsigned int
data type. It occupies 4 bytes on the 32-bit platform and 8 bytes on the 64-bit platform regardless of the data size it points to. You may think of memory as an array of bytes and pointer is an index to the array.
In order to construct the object you need a storage. You may think of storage as a region in the memory. What is the size of the object? The answer is sizeof(A)
. On 64 bit platform sizeof(A) == 16
int
variableint
The storage is an addressable memory you can get a pointer:
Platform neutral
Platform specific
So when you construct the object on the stack, the compiler allocates the required amount of memory on the stack and invokes the class constructor.
When you construct the object on the heap using the operator new
, the compiler invokes a malloc
-like function to allocate some amount of the memory on the heap and then it invokes the class constructor.
In case if object is allocated on the stack, compiler ensures it is destroyed once you return from the function (or leave the scope they say). The same behavior is for the objects allocated on the static storage.
In case if object is allocated on the heap, it is developer's responsibility to destroy the object and release the memory.
In your case you perform two allocations on the heap:
class A
objectint
Because you never call operator delete
the objects you're created are never released. That is a memory leak.
Upvotes: 1
Reputation: 67447
So we have 2 main memories stack and heap
That's a silly thing to say, you just have memory. Be it from single or multiple RAM sticks, L* CPU caches, disk backed memory mapped files, swap files, tape, it's all memory. And the correct name for it is virtual memory in modern operating systems. Whether you choose to name parts of it "stack" or "heap" or "kittens", that's your choice.
So in above allocation we only need 4bytes of heap memory as only have single data member which is pointer.
No, you have sizeof(A)
(probably) equal to 2 * sizeof(int)
. On a 32-bit Intel system, that's (probably) 8 bytes. That's how much memory is allocated by the new
operator when called with A
.
weight= new int(100);
Unrelated to your question, but that most likely doesn't do what you think it does. It doesn't allocate an array of 100 int
items, it allocates one int
and initializes it to 100.
What about member functions where are they stored?
Functions are code, and code is stored in the text segment of your executable. It is then mapped to virtual memory by the OS executable loader and memory manager, allowing pointers to reference it.
Virtual member functions are different, a pointer to them is stored in a hidden allocation inside your class allocation. Did you happen to see all those "probably" above? The virtual function table is one reason for those, it makes the allocation larger than it would seem to be.
Upvotes: 0