Connecting Tech Pros Worldwide Help | Site Map

memory allocation.

  #1  
Old December 31st, 2005, 07:05 AM
siddhu
Guest
 
Posts: n/a
If i create array of int* on heap in the following way


{
int ** i = new int*[2];
int j = 0;
i[0]=&j;
i[1]=&j;
delete []i;
}

My question is, does the above code has any memory leak?

And if i do it in following way

{
int ** i = new int*[2];
int j = 0;
i[0]=new int(j);
i[1]=new int(j);
delete []i;
return 0;
}

Does "delete []i;" take care of everything?
Or should I do
delete i[0];delete i[1]; before i do delete []i; to avoid memory leak.

  #2  
Old December 31st, 2005, 07:35 AM
Greg
Guest
 
Posts: n/a

re: memory allocation.


siddhu wrote:[color=blue]
> If i create array of int* on heap in the following way
>
>
> {
> int ** i = new int*[2];
> int j = 0;
> i[0]=&j;
> i[1]=&j;
> delete []i;
> }
>
> My question is, does the above code has any memory leak?[/color]

No, because each new is matched with a corresponding delete.
[color=blue]
> And if i do it in following way
>
> {
> int ** i = new int*[2];
> int j = 0;
> i[0]=new int(j);
> i[1]=new int(j);
> delete []i;
> return 0;
> }
>
> Does "delete []i;" take care of everything?
> Or should I do
> delete i[0];delete i[1]; before i do delete []i; to avoid memory leak.[/color]

With three new's and only one delete, there is a memory leak since two
of the new's have no corresponding delete's.

This being a C++ newsgroup, just toss everything into a std::vector.
Then you will not have to worry about matching calls to new with
subsequent calls to delete.

Greg

  #3  
Old December 31st, 2005, 07:35 AM
Gianni Mariani
Guest
 
Posts: n/a

re: memory allocation.


siddhu wrote:[color=blue]
> If i create array of int* on heap in the following way
>
>
> {
> int ** i = new int*[2];
> int j = 0;
> i[0]=&j;
> i[1]=&j;
> delete []i;
> }
>
> My question is, does the above code has any memory leak?[/color]

one "new" ... one "delete" - no leak.
[color=blue]
>
> And if i do it in following way
>
> {
> int ** i = new int*[2];
> int j = 0;
> i[0]=new int(j);
> i[1]=new int(j);
> delete []i;
> return 0;
> }
>
> Does "delete []i;" take care of everything?[/color]

3 x "new" ... 1 x "delete" - yep, there 2 memory leaks.
[color=blue]
> Or should I do
> delete i[0];delete i[1]; before i do delete []i; to avoid memory leak.[/color]

In general, you need to pair new with delete until you start using smart
pointers.

e.g.

#include <memory>

int main()
{
std::auto_ptr<int> * i = new std::auto_ptr<int>[2];
int j = 0;
i[0].reset( new int(j) );
i[1].reset( new int(j) );
delete []i;
return 0;
}


  #4  
Old December 31st, 2005, 03:35 PM
Maxim Yegorushkin
Guest
 
Posts: n/a

re: memory allocation.



Gianni Mariani wrote:

[]
[color=blue]
> In general, you need to pair new with delete until you start using smart
> pointers.
>
> e.g.
>
> #include <memory>
>
> int main()
> {
> std::auto_ptr<int> * i = new std::auto_ptr<int>[2];
> int j = 0;
> i[0].reset( new int(j) );
> i[1].reset( new int(j) );
> delete []i;
> return 0;
> }[/color]

Bad example. This code is not exception safe. If any of new int()
throws i will leak.

  #5  
Old January 2nd, 2006, 05:15 AM
swesoc
Guest
 
Posts: n/a

re: memory allocation.




siddhu wrote:[color=blue]
> If i create array of int* on heap in the following way[/color]
[color=blue]
> {
> int ** i = new int*[2];
> int j = 0;
> i[0]=&j;
> i[1]=&j;
> delete []i;
> }[/color]

[color=blue]
> My question is, does the above code has any memory leak?[/color]

Here there are 2 pointers pointing to the same memory, now delete []i
will delete the 1st pointer while deleting the 2nd pointer, there will
be problem as that memory is already deleted result in a dangling
pointer.

And if i do it in following way


{
int ** i = new int*[2];
int j = 0;
i[0]=new int(j);
i[1]=new int(j);
delete []i;
return 0;



}


Does "delete []i;" take care of everything?
Or should I do
delete i[0];delete i[1]; before i do delete []i; to avoid memory leak.

Over here there will be no problem of neither memory leak or dangling
pointer

  #6  
Old January 2nd, 2006, 05:45 AM
bvatsa@gmail.com
Guest
 
Posts: n/a

re: memory allocation.


There is no problemwith the both code snippets, delete []i ,, will take
care of everything..

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic memory allocation stack heaps vivek answers 14 November 17th, 2007 04:45 AM
Dynamic Memory Allocation for Matrix(2D array) Peterwkc answers 1 April 22nd, 2007 06:23 AM
static memory allocation versus dynamic memory allocation Ken answers 24 November 30th, 2006 12:15 AM
Checking return values for errors, a matter of style? Johan Tibell answers 66 August 7th, 2006 06:45 PM
What is the difference between dynamic memory allocation,and stack allocation ? chris answers 6 October 28th, 2005 06:35 AM