Can someone find the error in this destructor?

This is a discussion on Can someone find the error in this destructor? within the C++ Programming forums, part of the General Programming Boards category; I don't know what's going on here, but its going to crap everytime. I'm just trying to clear all the ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    37

    Can someone find the error in this destructor?

    I don't know what's going on here, but its going to crap everytime. I'm just trying to clear all the memory like a good programmer.

    Code:
    #include "stack.h"
    #include <cstddef>
    
    Stack::Stack()
    {
    }
    
    Stack::~Stack()
    {
    	//while (!isEmpty())
    	//	pop();
    
    	StackNode *temp;
    
    	while (topOfStack != NULL)
    	{
    		temp = topOfStack;
    		topOfStack = topOfStack->next; // Seems to be a problem here
    		delete temp;
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    doesn't the ~ go beforehand like ~stack::stack

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Originally posted by Extol
    doesn't the ~ go beforehand like ~stack::stack
    no it doesnt
    it goes
    return type classname::function name
    like

    Code:
    int stack::sum(int x, int y);
    
    or
    cstack::cstack(); //constructors dont have return types
    nextus, the samurai warrior

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    this may or may not be it and im new at c++ but shouldnt it be:
    Code:
    #include "stack.h"
    #include <cstddef>
    
    void Stack::Stack()
    {
    }
    
    void Stack::~Stack()
    {
    	//while (!isEmpty())
    	//	pop();
    
    	StackNode *temp;
    
    	while (topOfStack != NULL)
    	{
    		temp = topOfStack;
    		topOfStack = topOfStack->next; // Seems to be a problem here
    		delete temp;
    	}
    }
    (note the void return types) because you previously did not have them.... or are they not required in class embedded functions? i dunno because im new at this
    also this is a linked list correct? shouldnt it be temp = temp->next;
    ?? or rather topofstack = temp?

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    constructors and destructors dont have return types
    nextus, the samurai warrior

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Seems like you're just deleting temp, or something.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    I want to delete all of the nodes on the stack. Here's the header file I use with it:

    Code:
    typedef int StackType;
    
    class Stack
    {
    public:
    	Stack();
    	~Stack();
    	bool isEmpty();
    	void push(StackType[2]);
    	void pop(StackType[2]);
    
    
    private:
    	struct StackNode
    	{
    		StackType info[2];   //data item on the stack
    		StackNode *next;   //a pointer to the next node
    	};
    
    	StackNode *topOfStack = new StackNode;  //a pointer to the first node in stack
    };
    When I debug the implementation file (in first post) it fails right where I noted.
    Last edited by skanxalot; 03-03-2003 at 10:27 PM.

  8. #8
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Code:
    while (topOfStack != NULL && topOfStack->next != NULL)
    {
      temp = topOfStack;
      topOfStack = topOfStack->next; // Seems to be a problem here
      delete temp;
    }
    you forgot to check if topOfStack->next even points to anything
    plus you'll want to initialize the topOfStack->next to either point to something, or else = NULL (0)
    One other thing is it's probably better to put your initialization for topOfStack into your constructor; and also to check for errors, i.e.:
    Code:
    <snip>
    	StackNode *topOfStack;  //a pointer to the first node in stack
    };
    Code:
    Stack::Stack()
    {
      try {
        topOfStack = new StackNode;
      } catch(...) {
        topOfStack = NULL;
      }
      if(topOfStack != NULL) topOfStack->next = NULL;
    }
    or use 0 instead of NULL -
    same thing, although some say it's better to use NULL, and some say it's better to use 0... beats me but they both work.
    Plus it's easier to say
    if(topOfStack)
    than it is to say
    if(topOfStack != NULL)


    Also, if all you do in your catch(...) statement is set topOfStack to NULL or 0, then later you will have to be sure to always check if it points to anything, or if it is NULL.
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    37
    Cheers mate!

  10. #10
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    np - l8r m8
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Destructor called upon creation
    By Overlord in forum C++ Programming
    Replies: 14
    Last Post: 04-23-2008, 12:46 AM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21