0

suppose that Box is a class

now
Box mybox = new Box();

this first creates a reference variable and then mybox is dynamically allocated memory

but what difference does it create in allocating memory to a class instance dynamically or during compile time?
here what is the advantage of such memory allocation ?

we havent used such allocation in c++

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by IsharaComix
0

but what difference does it create in allocating memory to a class instance dynamically or during compile time?
here what is the advantage of such memory allocation ?

A program that allocates memory during runtime is more scalable.

Let's say you're making a spreadsheet application, and each cell is an object. If you allocate the memory to hold the cells statically (such as an array of structs in C), then your spreadsheet has a hardcoded maximum size that can only be altered by recompiling the source. If it's dynamically allocated, you can theoretically make as many cells as your computer can handle.

On the flip side, if you allocate a whole bunch of space statically, then if the user only uses 10% of that space, the other 90% is being wasted, since they can't deallocate it. Furthermore, if the computer that it's running on can't handle the maximum amount of space that's statically allocated, then the program can't run at all.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.