Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-10-2005, 11:28 AM   #1 (permalink)
MealMan401
Registered User
 
MealMan401's Avatar
 
Join Date: Apr 2004
Posts: 10
MealMan401 is on a distinguished road
really stuck on deallocation problem

Hey, I am stuck with something that i don't usually get stuck with. So i am thinking that either i have stumbled across something gotcha quality with the operator delete that i did not know about before, or that the problem is staring me in the face and i just can't see it!!!!!

this is the problem:
I have an array which i allocated. Each item in the array has a linked list tied to it.

So to delete I iterate through the array, and at each index i deallocate the linked list.
and at the end of that, i delete the array with delete [] array

I have done this very same thing many times before. So i would think i know what I am doing,
but apprently i don't because this is the message i get on the very first index while deallocating its linked list:
*** glibc detected *** free(): invalid pointer: 0x0804ee70 ***
Aborted

Also, if i skip the deallocation of the linked lists, and just try deallocating the array with delete [] array, i get almost the same message (with a different pointer value):
*** glibc detected *** double free or corruption (!prev): 0x0804fa70 ***
Aborted

here is the entire code ,the functions that pertain are:
void Mygraph::deletelist()
void file_createlist(const char* filename);
void string_createedges(int v, string* str_edges);

thank you very much to whoever figures it out!!!!

Code:
/////////////.h//////////////////////// #include <ostream> #include <fstream> #include <iostream> //#include "Mystack.h" using namespace std; struct EdgeData { int weight; struct Node* dest; }; struct Edge { struct EdgeData data; Edge* nextedge; }; struct NodeData { int nodenumber; }; struct Node { NodeData data;//container holding node data struct Edge* edges;//list of directed edges connecting vertex (node) to other vertices }; class Mygraph { private: Node* vlist; int size; public: Mygraph(); ~Mygraph(); int getsize(); void printlist(ostream& out); void deletelist(); bool inorderpath(int source, int dest, bool *visited ,ostream& out); //breadthprint(ostream& out); void file_createlist(const char* filename); void string_createedges(int v, string* str_edges); }; /////////////.cpp////////// Mygraph::Mygraph() { vlist = 0; size = 0; } Mygraph::~Mygraph() { deletelist(); } void Mygraph::printlist(ostream& out) { Edge* edge = 0; out<<"Adjacency List has "<<size<<" vertices"<<endl; for(int i = 0; i < size; i++) { out<<vlist[i].data.nodenumber<<"\t"; edge = vlist[i].edges; while(edge) { out<<edge->data.dest->data.nodenumber<<" "; edge = edge->nextedge; } out<<endl; } } int Mygraph::getsize() { return size; } void Mygraph::deletelist() { Edge *nextedge; Edge *doomed; for(int i = 0; i < size; i++) { doomed = vlist[i].edges; while(doomed) { cout<<"deleting..."<<endl; nextedge = doomed->nextedge; //delete doomed; doomed = nextedge; } } delete [] vlist; } bool Mygraph::inorderpath(int source, int dest, bool *visited ,ostream& out) { Edge* edge = 0; bool found = false; //check to see if this node has been visited if( !visited[source] ) { //check to see if the this node is the destination node if(vlist[source].data.nodenumber == dest) { found = true; cout<<"Cool..we found a path to "<<dest<<endl; //stack.push(source); } //mark it as visited visited[source] = true; //go through all nodes connected to this one edge = vlist[source].edges; while(edge && !found) { found = inorderpath(edge->data.dest->data.nodenumber,dest, visited,out); if(found)//last node visited was the destination node { //stack.push(source); cout<<"Currently visiting "<<vlist[source].data.nodenumber<<" on path back from "<<dest<<endl; } edge = edge->nextedge; } } return found; } //void Mygraph::breadthprint(ostream& out); void Mygraph::file_createlist(const char* filename) { string stringbuf; fstream file; int vindex = 0; file.open(filename,ios::in); if(file.fail()) { vlist = 0; return; } //matrix is n by n so find number //of vertices to make array. getline(file,stringbuf); size = stringbuf.length() - 31; vlist = new Node[size]; string_createedges(vindex,&stringbuf); for(vindex = 1; !file.eof(); vindex++) { getline(file,stringbuf); if(file.eof()) break; vlist[vindex].data.nodenumber = vindex; string_createedges(vindex,&stringbuf); } file.close(); return; } void Mygraph::string_createedges(int v, string* str_edges) { Edge** stitch = &(vlist[v].edges); *stitch = 0; int strlength = str_edges->length(); for(int i = 0; i < strlength; i+=2) { if( (*str_edges)[i] == '1') { *stitch = new Edge; (**stitch).data.dest = &(vlist[i/2]); stitch = &((**stitch).nextedge); *stitch = 0;//tie up end } } }
__________________

Last edited by Valmont : 04-10-2005 at 02:28 PM.
MealMan401 is offline   Reply With Quote
Old 04-10-2005, 02:28 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Make a main() to demonstrate the usage of such nature that the problem occurs. If data needs to be loaded for file(s) then post (or upload) them too please.

This will save me time.
__________________
Valmont is offline   Reply With Quote
Old 04-10-2005, 03:10 PM   #3 (permalink)
MealMan401
Registered User
 
MealMan401's Avatar
 
Join Date: Apr 2004
Posts: 10
MealMan401 is on a distinguished road
OK i am reposting the code because I changed the layout a bit.

///////////structs.h/////////////

Code:
#ifndef STRUCTS_H #define STRUCTS_H struct EdgeData { int weight; struct Node* dest; }; struct Edge { EdgeData data; Edge* nextedge; }; struct NodeData { int nodenumber; }; struct Node { NodeData data;//container holding node data Edge* edges;//list of directed edges connecting vertex (node) to other vertices }; struct Item { struct Node data; Item* prev; Item* next; }; #endif



////////myGraph.h////////////

Code:
#ifndef MYGRAPH_H #define MYGRAPH_H #include <ostream> #include <iostream> #include <fstream> #include "structs.h" using namespace std; class Mygraph { private: Node* vlist; int size; public: Mygraph(); ~Mygraph(); int getsize(); Node getnode(int index); void printlist(ostream& out); void deletelist(); bool inorderpath(int source, int dest, bool *visited ,ostream& out); //breadthprint(ostream& out); void file_createlist(const char* filename); void string_createedges(int v, string* str_edges); }; #endif


////Mygraph.cpp////////////

Code:
#include "Mygraph.h" Mygraph::Mygraph() { vlist = 0; size = 0; } Mygraph::~Mygraph() { deletelist();//BIG PROBLEM!!!!!!!!!!!! } void Mygraph::printlist(ostream& out) { Edge* edge = 0; out<<"Adjacency List has "<<size<<" vertices"<<endl; for(int i = 0; i < size; i++) { out<<vlist[i].data.nodenumber<<"\t"; edge = vlist[i].edges; while(edge) { out<<edge->data.dest->data.nodenumber<<" "; edge = edge->nextedge; } out<<endl; } } int Mygraph::getsize() { return size; } void Mygraph::deletelist() { Edge *nextedge; Edge *doomed; for(int i = 0; i < size; i++) { doomed = vlist[i].edges; while(doomed) { cout<<"deleting..."<<endl; nextedge = doomed->nextedge; delete doomed; doomed = nextedge; } } delete [] vlist; } bool Mygraph::inorderpath(int source, int dest, bool *visited ,ostream& out) { Edge* edge = 0; bool found = false; //check to see if this node has been visited if( !visited[source] ) { //check to see if the this node is the destination node if(vlist[source].data.nodenumber == dest) { found = true; cout<<"Cool..we found a path to "<<dest<<endl; //stack.push(source); } //mark it as visited visited[source] = true; //go through all nodes connected to this one edge = vlist[source].edges; while(edge && !found) { found = inorderpath(edge->data.dest->data.nodenumber,dest, visited,out); if(found)//last node visited was the destination node { //stack.push(source); cout<<"Currently visiting "<<vlist[source].data.nodenumber<<" on path back from "<<dest<<endl; } edge = edge->nextedge; } } return found; } //bool Mygraph::breadthpath(int source, int dest, bool* visited,ostream& out) //{ //} void Mygraph::file_createlist(const char* filename) { string stringbuf; fstream file; int vindex = 0; file.open(filename,ios::in); if(file.fail()) { vlist = 0; return; } //matrix is n by n so find number //of vertices to make array. getline(file,stringbuf); size = stringbuf.length() - 31; vlist = new Node[size]; string_createedges(vindex,&stringbuf); for(vindex = 1; !file.eof(); vindex++) { getline(file,stringbuf); if(file.eof()) break; vlist[vindex].data.nodenumber = vindex; string_createedges(vindex,&stringbuf); } file.close(); return; } void Mygraph::string_createedges(int v, string* str_edges) { Edge** stitch = &(vlist[v].edges); *stitch = 0; int strlength = str_edges->length(); for(int i = 0; i < strlength; i+=2) { if( (*str_edges)[i] == '1') { *stitch = new Edge; (**stitch).data.dest = &(vlist[i/2]); stitch = &((**stitch).nextedge); *stitch = 0;//tie up end } } } Node Mygraph::getnode(int index) { return vlist[index]; }

/////////main.cpp/////////////

Code:
#ifndef MAIN_CPP #define MAIN_CPP #include <iostream> #include <string> #include <fstream> #include "structs.h" #include "Mygraph.h" using namespace std; int main(int argc, char **argv) { Mygraph graph; graph.file_createlist("input_graph.txt"); graph.printlist(cout); return 0; } #endif
///////////input_graph.txt/////////// is 31*31 wide with entries seperated with tabs.
Code:
0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0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With Quote
Old 04-10-2005, 03:14 PM   #4 (permalink)
MealMan401
Registered User
 
MealMan401's Avatar
 
Join Date: Apr 2004
Posts: 10
MealMan401 is on a distinguished road
AAAAAAhhh what the heck, now it seems to work when i run it........


I did not change anything at all that had to do with the Mygraph class........


why would glibc choke on deleting a simply array and linked lists, and then change its mind?
__________________
MealMan401 is offline   Reply With Quote
Old 04-10-2005, 06:10 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
The fact that it runs, is never ever a prove that correct deletion occured!
Convince yourself it is correct indeed.

You can do that by creating a static member which holds the number of allocations on the freestore:
Code:
{ ... public: static unsigned EdgeCounter_; }; //Later... *stitch = new Edge ++EdgeCounter_; //...
Outside the classs:
Code:
unsigned Mygraph::EdgeCounter = 0;
And then in the delete function (deletelist())
Code:
void Mygraph::deletelist() { Edge *nextedge; Edge *doomed; for(int i = 0; i < size; i++) { doomed = vlist[i].edges; while(doomed) { cout<<"deleting..."<<endl; nextedge = doomed->nextedge; delete doomed; cout<<--EdgeCounter_<<endl; doomed = nextedge; } } //INSERT BREAKPOINT HERE delete [] vlist; }
Quote:
why would glibc choke on deleting a simply array and linked lists, and then change its mind?
See if you have the latest version.
__________________

Last edited by Valmont : 04-10-2005 at 06:36 PM.
Valmont is offline   Reply With Quote
Old 04-10-2005, 08:58 PM   #6 (permalink)
MealMan401
Registered User
 
MealMan401's Avatar
 
Join Date: Apr 2004
Posts: 10
MealMan401 is on a distinguished road
OK, I see what you are saying about keeping track of how many times that piece of code runs, and the implications of it not being correct.

But the thing that I am confused about is the specific message that I got in the console. Usually if you try to delete something that you don't own then you simply get the message "Segmentation fault". But the message was " *** glibc detected *** free(): invalid pointer: 0x0804ee70 *** Aborted " for the attempted deletion of a edge (which is just a struct). And " *** glibc detected *** double free or corruption (!prev): 0x0804fa70 ***
Aborted
for the attempted deletion of the array.

I googled for a while trying to find a clear explanation of what those error messages were, but to no avail.
__________________
MealMan401 is offline   Reply With Quote
Old 04-11-2005, 02:18 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
For glibc related question, best is to contact the devs. Check out the GNU community and find your way there.

However:
Quote:
Usually if you try to delete something that you don't own then you simply get the message "Segmentation fault".
The ISO C++ standard does not guarantee that! When it comes to things like freestore management or things like overruns, you're on your own. No help from the standard there. So don't assume anything about compilers or how a program runs.
__________________
Valmont is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP code problem j.gohel Java 7 04-15-2005 02:07 PM
PHP problem with a re-direct googs PHP 8 03-01-2005 04:12 PM
Hashing problem jodders Standard C, C++ 1 02-09-2005 01:51 PM
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 07:59 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM


All times are GMT -8. The time now is 09:11 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle