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 9165685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:57:50+00:00 2026-06-17T14:57:50+00:00

I am working on a homework assignment for a C++ class, and having some

  • 0

I am working on a homework assignment for a C++ class, and having some difficulties. My program is segfaulting due to “use of uninitialised value of size 8” and “invalid read of size 8” in item::name() which is called by item::operator=.

#ifndef ITEM_H
#define ITEM_H

const double WEIGHT_DEFAULT = 1.0;
const int ITEM_NAME_LENGTH = 30;

class item {
public:
    // Constructors and deconstructor
    item();
    item(char* nme, double weight);
    virtual ~item();

    // Copy constructor
    item(const item& itm);

    // Overload assignment operator
    const item& operator=(const item& itm);

    // Overload inequality operator
    bool operator!=(const item&) const;

    // Mutators and accessors
    const char* name(void) const;
    double weight(void) const;
    void weight(double wght);
    void name(char* nme);

protected:
private:
    char* m_name;
    double m_weight;
};

#endif // ITEM_H

Here is the portion of the implementation that I think is having issues:

... 

const item& item::operator=(const item& itm)
{
    if (strcmp(this->name(), itm.name()) != 0)
    {

        this->weight(itm.weight());
        strcpy(m_name, itm.name());
    }

    return *this;
}

const char* item::name(void) const {
    return m_name;
}

...

As you can see, part of the assignment is to use c-strings, thus I’m stuck with the messy details. My understanding of pointers and classes is that when working with both, we need to implement destructors, copy constructors, and overload the assignment operator. I’ve done all of the above. I looked at some info on the copy-and-swap idiom here on Stack Overflow, and tried implementing that, but couldn’t get that to work with my code either.

My brain is getting frazzled trying to puzzle out where I went wrong. Could someone please tell me what I’m missing?

Thanks!


EDIT

Here are the constructors, destructor, and such:

item::item() {
    //ctor

    m_name = new char[ITEM_NAME_LENGTH];
    strncpy(m_name, " ", ITEM_NAME_LENGTH - 1);
    this->weight(WEIGHT_DEFAULT);

    return;
}

item::item(char* nme, double wght) {
    m_name = new char[ITEM_NAME_LENGTH];

    strncpy(m_name, nme, ITEM_NAME_LENGTH - 1);

    // We want to check for a rational value being
    // passed in to weight. In theory, an item has
    // to have *some* kind of weight, so we check
    // for a weight of 0. If a weight of 0 has been passed in,
    // we instead initialize the item's weight to the
    // default weight (set in the header file).
    if (wght > 0.0) {
        this->weight(wght);
    } else {
        this->weight(WEIGHT_DEFAULT);
    }

    return;
}

item::~item() {
    //dtor

    // TODO: We need to clean up any variables here.
    delete[] m_name;
}

item::item(const item& itm) {
    // copy ctor
    this->weight(itm.weight());
    strncpy(m_name, itm.name(), ITEM_NAME_LENGTH - 1);

}

const item& item::operator=(const item& itm)
{
    if (strcmp(this->name(), itm.name()) != 0)
    {

        this->weight(itm.weight());
        strcpy(m_name, itm.name());
    }

    return *this;
}

bool item::operator!=(const item& itm) const
{
    return (strcmp(m_name, itm.name()) != 0);
}

EDIT 2

Now that I’ve stopped trying to dynamically allocate the name c-string, I’m getting memory errors on the weight function…

double item::weight(void) const {
    return m_weight;
}

void item::weight(double wght)
{
    m_weight = wght;
}
  • 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-17T14:57:51+00:00Added an answer on June 17, 2026 at 2:57 pm

    First off, you don’t need to explicitly return; from void statements.

    Second:
    When creating a copy constructor, you need to allocate the space that will be used to hold the memory of that constructor, the same way you do for the regular constructor.

    Try this in your copy constructor:

    m_name = new char[ITEM_NAME_LENGTH];
    

    Edit: for completeness this is what my “appears to be working” code is:

    #include <stdio.h>
    #include <string.h>
    
    
    const double WEIGHT_DEFAULT = 1.0;
    const int ITEM_NAME_LENGTH = 30;
    
    class item {
    public:
        // Constructors and deconstructor
        item();
        item(char* nme, double weight);
        virtual ~item();
    
        // Copy constructor
        item(const item& itm);
    
        // Overload assignment operator
        const item& operator=(const item& itm);
    
        // Overload inequality operator
        bool operator!=(const item&) const;
    
        // Mutators and accessors
        const char* name(void) const;
        double weight(void) const;
        void weight(double wght);
        void name(char* nme);
    
    protected:
    private:
        char* m_name;
        double m_weight;
    };
    
    
    const item& item::operator=(const item& itm)
    {
        if (strcmp(this->name(), itm.name()) != 0)
        {
    
            m_name = new char[ITEM_NAME_LENGTH];
            this->weight(itm.weight());
            strcpy(m_name, itm.name());
        }
    
        return *this;
    }
    
    const char* item::name(void) const {
        return m_name;
    }
    
    double item::weight(void) const {
        return m_weight;
    }
    
    void item::weight(double wght) {
        m_weight = wght;
    }
    
    item::item() {
        //ctor
    
        m_name = new char[ITEM_NAME_LENGTH];
        strncpy(m_name, " ", ITEM_NAME_LENGTH - 1);
        this->weight(WEIGHT_DEFAULT);
    
        return;
    }
    
    item::item(char* nme, double wght) {
        m_name = new char[ITEM_NAME_LENGTH];
    
        strncpy(m_name, nme, ITEM_NAME_LENGTH - 1);
    
        // We want to check for a rational value being
        // passed in to weight. In theory, an item has
        // to have *some* kind of weight, so we check
        // for a weight of 0. If a weight of 0 has been passed in,
        // we instead initialize the item's weight to the
        // default weight (set in the header file).
        if (wght > 0.0) {
            this->weight(wght);
        } else {
            this->weight(WEIGHT_DEFAULT);
        }
    
        return;
    }
    
    item::~item() {
        //dtor
    
        // TODO: We need to clean up any variables here.
        delete[] m_name;
    }
    
    item::item(const item& itm) {
        // copy ctor
        this->weight(itm.weight());
        m_name = new char[ITEM_NAME_LENGTH];
        strncpy(m_name, itm.name(), ITEM_NAME_LENGTH - 1);
    
    }
    
    bool item::operator!=(const item& itm) const
    {
        return (strcmp(m_name, itm.name()) != 0);
    }
    
    
    int main(int argc, char* argv[])
    {
        item i("test",2.0);
        item b = i;
        item c;
        item d = c;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a homework assignment, I'm working with the following. It's an assigned class structure,
I'm working on a homework assignment in which I'm required to use char arrays
So, I'm working on a homework assignment, and I'm having a hard time following
I'm working on a homework assignment translating a C program we wrote to MIPS.
I am working on a homework assignment where we aren't allowed to use any
Some classmates and I are working on a homework assignment for Java that requires
First time poster, yadayada. Working on a homework assignment and I'm having an issue
I am working on a homework assignment for a class. The problem statement says
I'm working on a homework assignment for Java, where a program is supposed to
So in my java class we have a homework assignment to use System.currentTimeMillis to

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.