
December 31st, 2005, 07:05 AM
| | | memory allocation.
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. | 
December 31st, 2005, 07:35 AM
| | | 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 | 
December 31st, 2005, 07:35 AM
| | | 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;
} | 
December 31st, 2005, 03:35 PM
| | | 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. | 
January 2nd, 2006, 05:15 AM
| | | 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 | 
January 2nd, 2006, 05:45 AM
| | | Re: memory allocation.
There is no problemwith the both code snippets, delete []i ,, will take
care of everything.. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|