why allocate memory??

Hi I'm just finishing reading Alex Allains book and he doesn't give too much of examples on why and where we would allocate memory dynamically,

so the question why and when do we allocate memory dynamically from the free store?

and also why can we only allocate memory to pointers and not objects and variables for example why would this line of code not work?

 
  int x = new int;


Thanks,

Adam
closed account (E0p9LyTq)
Why use dynamic memory? When you need a lot of space.

When do I need dynamic memory
http://stackoverflow.com/questions/12161774/ddg#12161952
why would this line of code not work?
new int returns int * while x is only int.

why and where we would allocate memory dynamically,
Besides what FurryGuy said, it is also for controling the life time of an object. The life time of a local variable ends when the function ends.
Topic archived. No new replies allowed.