473,473 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.

Dec 31 '05 #1
5 1108
siddhu wrote:
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?
No, because each new is matched with a corresponding delete.
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.


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

Dec 31 '05 #2
siddhu wrote:
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?
one "new" ... one "delete" - no 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?
3 x "new" ... 1 x "delete" - yep, there 2 memory leaks.
Or should I do
delete i[0];delete i[1]; before i do delete []i; to avoid memory leak.


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;
}
Dec 31 '05 #3

Gianni Mariani wrote:

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


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

Dec 31 '05 #4


siddhu wrote:
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?


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

Jan 2 '06 #5
There is no problemwith the both code snippets, delete []i ,, will take
care of everything..

Jan 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.