Connecting Tech Pros Worldwide Help | Site Map

memory allocation.

siddhu
Guest
 
Posts: n/a
#1: Dec 31 '05
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.

Greg
Guest
 
Posts: n/a
#2: Dec 31 '05

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

Gianni Mariani
Guest
 
Posts: n/a
#3: Dec 31 '05

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;
}


Maxim Yegorushkin
Guest
 
Posts: n/a
#4: Dec 31 '05

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.

swesoc
Guest
 
Posts: n/a
#5: Jan 2 '06

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

bvatsa@gmail.com
Guest
 
Posts: n/a
#6: Jan 2 '06

re: memory allocation.


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

Closed Thread