int array[10] == int* array = new int[10] ?

Jul 18, 2009 at 12:46am
I'm just learning C++ for the first time. I have been programing in java for about 2 - 3 years and C for about 1 year. I'm wondering what is the difference between

int array[10];

and

int* array = new int[10];
Jul 18, 2009 at 1:06am
The 10 integers are placed on the stack in the first case.

In the second case, the 10 integers are allocated on the heap and a pointer to the array is placed on the stack.
Jul 18, 2009 at 2:03am
What exactly does that mean tough? I never fully understood how the stack and the heap worked.
Jul 18, 2009 at 2:20am
In the first case, the array will automatically be destroyed/freed after you exit the scope.

In the second case, you will have to delete it manually using delete array; however, the pointer WILL be destroyed after you exit the scope, which means you won't be able to delete the memory if you don't have a pointer to it.
Jul 18, 2009 at 3:53am
Thanks that helps
Jul 18, 2009 at 12:50pm
Another difference is that the stack is much more limited in size than the heap.
Topic archived. No new replies allowed.