473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

delete dynamically allocated memory in a list?

Yi
Two questions about the following code sample:

--- code begins ---
//class IPv4 is defined elsewhere

list<IPv4ip_list;

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

//IPv4 new_ip(addr);
//ip_list.push_back(new_ip);
ptr = new IPv4(addr);
ip_list.push_back(*ptr);
}

....

list<IPv4>::iterator k;
k = ip_list.begin();
while (k!=pp_list.end()) {
delete &pp_list.front();
pp_list.pop_front();
k = pp_list.begin();
}

--- code ends ---

When I try to free the memory using delete, the program run into error
saying "double free or corruption". I don't know why. How am I supposed
to delete the dynamically allocated memory in a list?

By the way, if I use the two statement that are currently commented in
the first for loop instead of the two statements below them, I don't
need to worry about freeing the memory, right?

Thanks!

Aug 23 '06 #1
7 2264

Yi wrote:
Two questions about the following code sample:

--- code begins ---
//class IPv4 is defined elsewhere

list<IPv4ip_list;

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

//IPv4 new_ip(addr);
//ip_list.push_back(new_ip);
ptr = new IPv4(addr);
ip_list.push_back(*ptr);
Note that here a *copy* of *ptr is pushed into the list. It is not the
same object.

}

...

list<IPv4>::iterator k;
k = ip_list.begin();
while (k!=pp_list.end()) {
delete &pp_list.front();
This does not fit here. Your list element is an object and not a
pointer. So you should not be deleting it.
pp_list.pop_front();
k = pp_list.begin();
}

--- code ends ---

When I try to free the memory using delete, the program run into error
saying "double free or corruption". I don't know why. How am I supposed
to delete the dynamically allocated memory in a list?
You should do a delete ptr above and not delete a list element. They
are different objects.
Hope this helps.

Aug 23 '06 #2
Yi
Thanks a lot Vikram!
>
You should do a delete ptr above and not delete a list element. They
are different objects.
Do you mean the following?

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

//IPv4 new_ip(addr);
//pp_list.push_back(new_ip);
ptr = new IPv4(addr);
pp_list.push_back(*ptr);
delete ptr;
}
....

As I asked in the previous post, is it simpler to just use the
following code?

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

IPv4 new_ip(addr);
pp_list.push_back(new_ip);
//ptr = new IPv4(addr);
//pp_list.push_back(*ptr);
//delete ptr;
}

Thanks a lot!

Aug 23 '06 #3

Yi wrote:
You should do a delete ptr above and not delete a list element. They
are different objects.

Do you mean the following?

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

//IPv4 new_ip(addr);
//pp_list.push_back(new_ip);
ptr = new IPv4(addr);
pp_list.push_back(*ptr);
delete ptr;
}
...

As I asked in the previous post, is it simpler to just use the
following code?

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

IPv4 new_ip(addr);
pp_list.push_back(new_ip);
//ptr = new IPv4(addr);
//pp_list.push_back(*ptr);
//delete ptr;
}
Yes...either of the above is fine. In one case you take care of delete
and in the second (which is more convenient) the local variable cleanup
gets automatically done. I would personally go with the second approach
( new_ip).

Aug 23 '06 #4
Yi
Yeah, I agree!

And one more dumb question: since the scope of new_ip is within the
pair of { }, there is no problem of repeated definitions among
iterations, right?
for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

IPv4 new_ip(addr);
pp_list.push_back(new_ip);
//ptr = new IPv4(addr);
//pp_list.push_back(*ptr);
//delete ptr;
}

Yes...either of the above is fine. In one case you take care of delete
and in the second (which is more convenient) the local variable cleanup
gets automatically done. I would personally go with the second approach
( new_ip).
Aug 23 '06 #5
On 22 Aug 2006 23:09:39 -0700 in comp.lang.c++, "Yi"
<wy****@gmail.comwrote,
>And one more dumb question: since the scope of new_ip is within the
pair of { }, there is no problem of repeated definitions among
iterations, right?
Right, it is automatically created and destroyed for each iteration,
but only one at a time.

Aug 23 '06 #6
Yi wrote:
As I asked in the previous post, is it simpler to just use the
following code?

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

IPv4 new_ip(addr);
pp_list.push_back(new_ip);
//ptr = new IPv4(addr);
//pp_list.push_back(*ptr);
//delete ptr;
}
Why not do it in one line?

pp_list.push_back(addr);

There is probably no need to have new_ip.

HTH,
Michiel Salters

Aug 23 '06 #7

Mi*************@tomtom.com wrote:
Yi wrote:
As I asked in the previous post, is it simpler to just use the
following code?

for (int i=1; i<=9; i++) {
char addr[128];
sprintf(addr, "%d.%d.%d.%d", i,i,i,i);

IPv4 new_ip(addr);
pp_list.push_back(new_ip);
//ptr = new IPv4(addr);
//pp_list.push_back(*ptr);
//delete ptr;
}

Why not do it in one line?

pp_list.push_back(addr);

There is probably no need to have new_ip.
If I wrote IPv4 i would probably have made the constructor explicit.
Thus:
pp_list.push_back(IPv4(addr));

/Peter

Aug 23 '06 #8

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

Similar topics

2
by: Chris E. Yoon | last post by:
I just want to hear people's opinions on this subject. My application has lots and lots of short-lived objects that use dynamic allocation/deallocation. After implementing its functionality, I...
14
by: A | last post by:
Hi, Consider this: char* ptr = "a string"; Is ptr a pointer to a dynamically created object for which i must use the delete operator to deallocate memory? I'm sure the "a string" part...
5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
13
by: Franklin Li | last post by:
Hi All, I find one example codes as below: int send() { char msg; ... delete msg; ..
1
by: akywy | last post by:
Two questions about the following code sample: --- code begins --- //class IPv4 is defined elsewhere list<IPv4> ip_list; for (int i=1; i<=9; i++) { char addr; ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
15
by: tom | last post by:
why delete the dynamically allocated memory twice causes an error, see the code below: int _tmain(int argc, _TCHAR* argv) { int *pi = new int(12); cout<<*pi; delete pi; delete pi; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.