why can't assign an int on the heap

I don't understand why this isn't valid all I'm doing is setting aside memory from the heap to an int.

 
  int b = new int;


or

 
  int b = new int(10);


yet I can declare a pointer and get memory from the heap for a pointer :s

 
  int *b = new int;
new always returns a pointer to the space allocated on the heap (otherwise, how are you going to address it?).

In the third snippet, you're not allocating a pointer. You're allocating an int. new is returning the address of the int that was allocated which is then assigned to the pointer *b.


Last edited on
Topic archived. No new replies allowed.