Reputation: 707
I understand that certain data type object have certain buffer size. E.g. a char is 1byte. So, when creating a self-defined class object,
a
?Creating a user-defined class instance:
Animal a; //stack memory
a.makeSound();
Animal *a = new Animal(); //heap memory
a->makeSound();
Upvotes: 1
Views: 1424
Reputation: 6342
How much memory is allocated to the object a?
Depends on Animal class definition.
Is the amount of memory allocated different if the object is created on stack, or heap?
No.
Is the amount of memory allocated fixed, or can be changed?
Depending upon 32-bit or 64-bit system and compiler specific implementations like padding etc, amount of memory allocated may vary.
Upvotes: 0
Reputation: 28872
Upvotes: 0
Reputation: 170479
In both cases at least sizeof(Animal)
bytes will be allocated.
In case of stack allocation some extra memory might be used for alignment. In case of heap memory some extra memory will likely be used for storing heap service data. You can influence the exact amount of memory by changing the class - for example for heap allocation you can define a custom operator new
for that class and make it allocate whatever you want amount of memory.
Upvotes: 3