Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int a[10];

or

int *p;

p=new int [10];

i declare array of size 10 and then again declare array using new operator


int a[10]//this also contain same memory space

p=new int[10] //this also contain same memory space.

what is difference between them.
Posted

They don't contain the same memory space - they contain the same amount of memory. But there is a big difference.

when you write
C++
int a[10];
You declare an array of ten integers on the stack.
When you write
C++
int *p;
p=new int [10];
You declare the same amount of space, but this time on the heap.

The difference is important: stack space is reclaimed when the function the memory was allocated in returns: the memory goes out of scope, and is automatically released. Heap space does not, it has to be explicitly released by the delete keyword.
There are two effects of this:
1) If you use new without using delete then your application consumes more and more memory, which it never lets go - eventually your machine runs out of memory and crashes. This is called a "memory leak".
2) Worse, if you allocate memory on the stack, and then pass that address outside the function, then memory is valid, but may be released:
C++
int* getP()
   {
   int a[10];
   return &(a[0]);
   }
Is valid code, but the memory that is returned will be reused by the next function you call, so the same memory will be used for two purposes. This is called a "hanging reference" and can cause horribly intermittent faults, or an old-fashioned "general protection fault".

[edit]Typo - "stack" for "heap" - OriginalGriff[/edit]
[edit2]Brain fart - "free" for "delete" - OriginalGriff[/edit2]
 
Share this answer
 
v3
Comments
CPallini 18-Apr-13 4:15am    
My 5.
Good point on 'returning the address of a possibly out-of-scope object'.
OriginalGriff 18-Apr-13 4:22am    
I didn't understand it when I first started with C so the number of times I pulled my hair out...:laugh:
C++
int a[10];

Statically declares the array: the array size is known at compile time, it is automatically allocated on the stack, the programmer has not the resposibility to cleanup the obtained memory (memory deallocation is automatically handled).

C++
int * p;
p = new int [10];

Dynamically allocates the array, (generally speaking) the array size is not known at compile time, (this is more evident if you write p = new int[k];, while int a[k]; is not allowed). The memory is allocated on the heap, the programmer has the responsibility to clean it up (using delete [] p;) otherwise a memory leak happens.


If you are not familiar with stack, heap, memory cleanup, memory leak concepts, then I strongly suggest you to fill such gaps in your knowledge.
 
Share this answer
 
Comments
nv3 18-Apr-13 4:03am    
My 5 to both of you.
CPallini 18-Apr-13 4:12am    
Than you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900