3

I was trying to change a ListNode struct into a class format but am running into some issues when testing it.

Getting a.out(7016) malloc: * error for object 0x7fff65333b10: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

chainLink.hpp 
#ifndef CHAINLINK_H
#define CHAINLINK_H

using namespace std;
#include <iostream>
#include <cstdlib>

template <typename Object>

class chainLink
{
    private:
        Object storedValue;
        chainLink *nextLink;
    public:
            //Constructor
        chainLink(const Object &value = Object()): storedValue(value)
        {
            nextLink = NULL;
        }
        /* Postcondition:   returns storedValue;
         */     
        Object getValue()
        {
            return storedValue;
        }

        /* Postcondition:   sets storedValue = value
         */
        void setValue(Object &value)
        {
            storedValue = value;
        }

        /* Postcondition:   sets nextLink to &value
         */
        void setNextLink(chainLink* next)
        {
            nextLink = next;
        }

        chainLink* getNext()
        {
            return nextLink;
        }
        ~chainLink()
        {
            delete nextLink;
        }
};
#endif

My test file, assume includes

int main()
{
    chainLink<int> x(1);
    cout << "X: " << x.getValue() << " "<< endl;
    chainLink<int> y(2);
    cout << "Y: " << y.getValue() << " "<< endl;
    chainLink<int>* z = &y;
    cout << &y << " " << z << endl;
    x.setNextLink(z);
}

OUTPUT: X: 1 Y: 2 0x7fff65333b10 0x7fff65333b10 a.out(7016) malloc: * error for object 0x7fff65333b10: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug Abort trap: 6

The error seems to be thrown by the setNextLink function.

Any help greatly appreciated.

Stals
  • 1,543
  • 4
  • 27
  • 52
user1645240
  • 55
  • 1
  • 7
  • 4
    It helps if you try to read and understand what the error message is telling you: "pointer being freed **was not allocated**." That probably means you forgot to allocate something, like say a `new` somewhere. Every `delete` call must be preceded by a `new` call somewhere (which may or may not be somewhere outside your code). – In silico Sep 04 '12 at 06:33
  • Did you get the `z`pointer from a call to new? – Mat Sep 04 '12 at 06:33
  • Not strictly related, but what do you mean by changing something "from struct to a class format"? – jalf Sep 04 '12 at 06:34
  • You have named your class `Object`. At first I thought it was JAVA code. In C++ it is common to use `T` as in `template `. Also method names typically start with a capital letter. – Hindol Sep 04 '12 at 06:35
  • 1
    @Hindol "Also method names typically start with a capital letter". No they don't. – juanchopanza Sep 04 '12 at 06:38
  • @juanchopanza At least in `Design Patterns` and `Modern C++ Design` they do. I thought it is commonplace. – Hindol Sep 04 '12 at 06:46
  • @Hindol There isn't a standard convention. The one used by the standard library or boost for example, is completely different again. Unfortunately, one has to get used to code with differing conventions. – juanchopanza Sep 04 '12 at 06:50

3 Answers3

1

In the last line of main you are calling setNextLink with a pointer to an object with automatic storage duration (z holds the address of y). Your list tries to delete that pointer when it is destroyed, hence the error (y has not been allocated dynamically, and hence cannot be deleted dynamically).

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
1

You are giving setNextLink a pointer to an automatically allocated variable,

x.setNextLink(z); // z points to automatic object y

which you attempt to delete in the constructor.

~chainLink() {
    delete nextLink; // attempts to delete automatic object y
}

You need to pass a pointer to a dynamically allocated object, or make your own insde your chainLink class.

Note: In C++, structs and classes are the same bar some differences. A equivalent types can be implemented using either of the two.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

After the line x.setNextLink(z); x.nextLink points to z, which in turn is pointing to y. But y is a local object. It is allocated on stack and not the heap. So it is illegal to call delete on it.

Hindol
  • 2,924
  • 2
  • 28
  • 41