Thread: Extremely strange "delete" problem

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    Extremely strange "delete" problem

    Hi All,

    I'm trying to make my own Stack Data Structure using classes. Unfortunately I ran into a very awkward problem that involves with the delete operator (No, I didn't free memory that was not supposed to be freed.)

    In my own implementation of the stack, I have inputStack and removeStack as my key member function of the class "LL_Stack".

    If you please take a look at the removeStack, I have commented out the statement :

    delete deletePtr

    Unfortunately, this totally messed up the entire output of the stack (ie. giving me weird output data). Below is the sample output without the delete statement and with the delete statement. I have pushed in the values: 4, 23, 21, 1000.

    With the delete statement

    1000
    134520848
    134520832
    134520864

    Without the delete statement

    1000
    21
    23
    4


    I'm not exactly sure what went wrong in my removeElement function since I am deleting the deletePtr at the end of the function and all of a sudden, the next time I execute that same function, the output went beserk.

    Is there something I'm missing in my code? Please take a look at the sample code below:

    Class Structure

    Code:
    class Node
    {
            public:
                    int data_info;
                    Node *nextPtr;
    
                    Node ()
                    {
                            data_info = 0;
                            nextPtr = NULL;
                    }
    
                    Node (int data, Node *def_ptr = NULL)
                    {
                            data_info = data;
                            nextPtr = def_ptr;
                    }
    
                    ~Node()
                    {
                            data_info;
                            delete nextPtr;
                    }
    };
    
    class LL_Stack
    {
            private:
                    Node *top_stack_ptr;
                    int num_elements;
    
            public:
                    LL_Stack()
                    {
                            top_stack_ptr = NULL;
                            num_elements = 0;
                    }
    
                    ~LL_Stack()
                    {
                            delete top_stack_ptr;
                            num_elements = 0;
                    }
    
                    void insertStack (int data_in);
                    void outputStack();
                    void removeStack();
                    int get_num_stack_elements();
    
    };
    Class Declarations

    Code:
    
    void LL_Stack :: outputStack()
    {
            Node *currentPtr;
    
            currentPtr = top_stack_ptr;
    
            cout << "The Following Stack Elements are: " << endl;
    
            while (currentPtr != NULL)
            {
                    cout << currentPtr->data_info << endl;
                    currentPtr = currentPtr->nextPtr;
            }
    
    }
    
    void LL_Stack :: insertStack (int data_in)
    {
            Node *newNode;
            newNode = new Node;
            newNode->data_info = data_in;
            newNode->nextPtr = NULL;
    
            if (top_stack_ptr == NULL)
            {
                    top_stack_ptr = newNode;
            }
            else
            {
                    newNode->nextPtr = top_stack_ptr;
                    top_stack_ptr = newNode;
            }
    
            num_elements += 1;
    
    }
    
    void LL_Stack :: removeStack()
    {
    
            Node *deletePtr;
    
            int retrieve_data;
    
            deletePtr = top_stack_ptr;
    
            retrieve_data = deletePtr->data_info;
    
            cout << retrieve_data << endl;
    
            top_stack_ptr = top_stack_ptr->nextPtr;
    
            //delete deletePtr;
    
    }
    
    int LL_Stack :: get_num_stack_elements()
    {
            return (num_elements);
    }
    Main Program

    Code:
    int main ()
    {
            LL_Stack A;
    
            A.insertStack (4);
    
            cout << "Number of elements: " << A.get_num_stack_elements() << endl;
    
            A.insertStack (23);
            A.insertStack (21);
            A.insertStack (1000);
    
    
            cout << "Number of elements: " << A.get_num_stack_elements() << endl;
    
    
            cout << "Elements in Stack: " << endl;
    
    
            A.outputStack();
    
            A.removeStack();
    
            A.removeStack();
            A.removeStack();
            A.removeStack();
    
    
    
    
            return (0);
    
    }
    I feel that not putting in the delete statement in my removeElement function will cause a major memory leak. If someone can please rectify this problem, it'll be greatly appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    can anyone please help?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An easy problem and an easy mistake! Take another look at your Node class:
    Code:
    class Node
    {
            public:
                    int data_info;
                    Node *nextPtr;
    
                    Node ()
                    {
                            data_info = 0;
                            nextPtr = NULL;
                    }
    
                    Node (int data, Node *def_ptr = NULL)
                    {
                            data_info = data;
                            nextPtr = def_ptr;
                    }
    
                    ~Node()
                    {
                            data_info;
                            //delete nextPtr; // <--- There is your problem!
                    }
    };
    What you're doing is that deleting a pointer referencing to the next object in the list! You're destroying deletePtr and deletePtr->nextPtr which would also be the current top_stack_ptr!
    I'm amazed the code didn't crash, though.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    Hi, thanks for your response.

    I thought destructors are used to delete useless objects from memory? I thought I knew everything about destructors but I guess I'm wrong.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course they are, for allocated memory.
    But in your case, you're using the pointer to reference other memory that was not allocated in that class. Be careful.
    A good rule is to let the function that created the memory release it, or use a memory manager.

    If you think top_stack_ptr in LL_Stack and nextPtr in Node is pointing to the same address when the destructor is called. Hence, after the destructor is finished, top_stack_ptr points to an invalid memory address (it's been freed).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  2. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  3. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM
  4. very annoying and strange problem.
    By Rare177 in forum Windows Programming
    Replies: 3
    Last Post: 12-15-2004, 08:01 AM
  5. Strange problem with fts_open call
    By jhopper in forum C Programming
    Replies: 0
    Last Post: 02-26-2002, 12:01 AM