473,322 Members | 1,526 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,322 software developers and data experts.

Link list destructor

Expand|Select|Wrap|Line Numbers
  1. struct Cell{
  2.    bool counted;
  3.    Cell* nextCell;
  4.    Cell(){ 
  5.        nextCell = NULL;
  6.    }
  7.    ~Cell(){
  8.       Cell* tmp = nextCell;
  9.       Cell* current_ptr = nextCell;
  10.       while(tmp != NULL){
  11.           current_ptr = current_ptr->nextCell;
  12.           delete tmp;
  13.           tmp = current_ptr;
  14.    }
  15. };
  16.  
  17. int main(){
  18.    Cell* a = new Cell;
  19.    Cell* b = new Cell;
  20.    Cell* c = new Cell;
  21.    a->nextCell = b; 
  22.    b->nextCell = c; 
  23.    delete a;   
  24.    return 0; 
  25. }
  26.  
It always get segmentation fault. Any idea?
Sep 5 '07 #1
4 2401
dmjpro
2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. struct Cell{
  2.    bool counted;
  3.    Cell* nextCell;
  4.    Cell(){ 
  5.        nextCell = NULL;
  6.    }
  7.    ~Cell(){
  8.       Cell* tmp = nextCell;
  9.       Cell* current_ptr = nextCell;
  10.       while(tmp != NULL){
  11.           current_ptr = current_ptr->nextCell;
  12.           delete tmp;
  13.           tmp = current_ptr;
  14.    }
  15. };
  16.  
  17. int main(){
  18.    Cell* a = new Cell;
  19.    Cell* b = new Cell;
  20.    Cell* c = new Cell;
  21.    a->nextCell = b; 
  22.    b->nextCell = c; 
  23.    delete a;   
  24.    return 0; 
  25. }
  26.  
It always get segmentation fault. Any idea?
What do you mean by segmentation fault?

Kind regards,
Dmjpro.
Sep 5 '07 #2
What do you mean by segmentation fault?

Kind regards,
Dmjpro.
Sorry I missed code line 7 in structor. It should be:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std; 
  5.  
  6. struct Cell{
  7.     list<int> vertices;    // Here is problem. 
  8.     bool counted;
  9.     Cell* nextCell;
  10.     Cell(){
  11.         counted=false; 
  12.         nextCell=NULL;
  13.     }
  14.     ~Cell(){
  15.         Cell* tmp = nextCell; 
  16.         Cell* current_ptr = nextCell; 
  17.         while(tmp != NULL){
  18.             current_ptr = current_ptr->nextCell;
  19.             delete tmp;
  20.             tmp = current_ptr;  
  21.         }
  22.     }
  23. };
  24.  
  25. int main(){
  26.     Cell* a = new Cell;
  27.     Cell* b = new Cell; 
  28.     Cell* c = new Cell; 
  29.     a->nextCell = b; 
  30.     b->nextCell = c;
  31.     delete a;
  32.     return 0; 
  33. }
  34.  
  35.  
It compiles, but when run this program, I get segmentation fault error message.
Sep 5 '07 #3
dmjpro
2,476 2GB
I went through the Net about Segmentation Fault.
It says me that, if i want to access impermissible memory location then the error comes.
You can get details on Net.
Try to understand yourself where you did wrong.

Kind regards,
Dmjpro.
Sep 5 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
There are so many things wrong here that I don't know wherte to begin. However, this code:
delete tmp;
tmp = current_ptr;
is causing your problem. Actually, the nextCell of the list never is set to 0 so you delete what you think is an allocation but isn't. Then you crash.

If you are trying to write a linked list as a homework assignment, then this is the wrong way to go. That is, a list presupposes a list class and a list has nodes (cells) so that presupposes a cell class. You can use structs but all of the data members must be private.

Next, a cell does not delete a list. A list deletes a list and a cell only deletes data that it has specifically allocated using the new operator. That is, the cell does not delete subsequent cells. That's because a cell should have no concept of a list. The list class has the concept oc the list and the list class should have no concept of a cell. Any changes the list needs to make to a cell member is done by calling a cell method.

Personally, I would ditch this code and start over. This time paying more attention to encapsulation.
Sep 5 '07 #5

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

Similar topics

11
by: Andrew Skouloudis | last post by:
Hello people and merry christmas. I am trying to create a simple linked list and it seems i am doing something wrong with the DeleteNode function ... #include <malloc.h> #include <stdio.h>...
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
5
by: sriram | last post by:
I have the following: class BSA { ... ... ... ... public: enum VRGAttrId {
2
by: barnesc | last post by:
>barnesc at engr.orst.edu wrote: > > So my question is: are there any other *practical* applications of a > > B-tree based list/set/dict ? In other words, is this module totally > > worth coding,...
7
by: berkay | last post by:
i have a txt file; berkay#white jack#black smith#jane writes in it. and after i run the program it only prints smith jane and crashes what is wrong?
2
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
3
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
5
by: cplusplusquestion | last post by:
#include <iostream> #include <list> using namespace std; struct Cell{ list<intvertices; bool counted; Cell* nextCell; Cell(){
4
by: Thomas Grund | last post by:
Hi, In the following code I would like that the class Node is only be used by the class Database. So the idea was to make the interface protected and use the friend definition. The code does not...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.