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++

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.