473,324 Members | 2,511 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,324 software developers and data experts.

delete dynamically allocated memory in a list?

1
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[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
1 1693
D_C
293 100+
Why do you have two lists, ip_list and pp_list? Also, you delete the pointer to the first list element, then instruct the program to remove the first list element whose pointer you just deleted? Why?
Aug 23 '06 #2

Sign in to post your reply or Sign up for a free account.

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; ..
7
by: Yi | last post by:
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; sprintf(addr, "%d.%d.%d.%d",...
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: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.