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