Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:23:28+00:00 2026-06-10T19:23:28+00:00

I was trying to change a ListNode struct into a class format but am

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:23:30+00:00Added an answer on June 10, 2026 at 7:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to change the text of a label from a class but have
Im trying to change the color of some of my cout outputs but after
I trying to change the column name of the DataTable but some how I
Am trying to change the button frame according to the orientation change but button
I'm trying to change some text in a .CSV file. StreamReader sReader = new
I was trying to change some parts of a joomla plugin, when I faced
I'm trying to change the font-size and color of a table cell, but it's
Im trying to change the title from the activities inside the Tab. but the
I´m trying to change the statusbar Color in storyboard, but it doesn´t work. The
I trying to change a website that is already up and running. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.