Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 17-Apr-13 22:35pm
ALIWAZ407
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

They don't contain the same memory space - they contain the same amount of memory. But there is a big difference.
 
when you write
int a[10];
You declare an array of ten integers on the stack.
When you write
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:
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]
  Permalink  
v3
Comments
CPallini at 18-Apr-13 4:15am
   
My 5.
Good point on 'returning the address of a possibly out-of-scope object'.
OriginalGriff at 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:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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).
 
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.
  Permalink  
Comments
nv3 at 18-Apr-13 4:03am
   
My 5 to both of you.
CPallini at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100