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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:43:26+00:00 2026-06-10T02:43:26+00:00

noob here. I’m doing an exercise from a book and the compiler reports no

  • 0

noob here. I’m doing an exercise from a book and the compiler reports no errors, but the program crashes when I try to run it.

I’m trying to run a little program exercising the different methods of the Cow class. It explicitly has: a constructor, a default constructor, a copy constructor, a destructor, an overloaded assignment operator, and a method to show its contents.

I’ll put the whole project:

Class specification:

//cow.h -- For project Exercise 12.1.cbp

class Cow
{
    char name[20]; // memory is allocated in the stack
    char *hobby;
    double weight;
public:
    Cow();
    Cow(const char *nm, const char *ho, double wt);
    Cow(const Cow &c);
    ~Cow();
    Cow & operator=(const Cow &c);
    void ShowCow() const; // display all cow data
};

Methods implementation:

// cow.cpp -- Cow class methods implementation (compile with main.cpp)

#include <cstring>
#include <iostream>
#include "cow.h"

Cow::Cow() // default destructor
{
    strcpy(name, "empty");
    hobby = new char[6]; // makes it compatible with delete[]
    strcpy(hobby, "empty");
    weight = 0.0;
}

Cow::Cow(const char *nm, const char *ho, double wt)
{
    strcpy(name, nm); // name = nm; is wrong, it copies the address of the argument pointer (swallow copying)
    /*if (name[20] != '\0') // if it's not a string, make it a string (in case nm is larger than 20)
        name[20] = '\0';*/
    hobby = new char[strlen(ho) + 1]; // allocates the needed memory to hold the argument string
    strcpy(hobby, ho); // copies the pointed-to data from the argument pointer to the class pointer
    weight = wt;
}

Cow::Cow(const Cow &c) // copy constructor
{
    strcpy(name, c.name); // copies the value to the desired address
    char *temp = hobby; // stores the address of the memory previously allocated with new
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby); // copies the value to the new address
    delete[] temp; // deletes the previously new allocated memory
    weight = c.weight;
}

Cow::~Cow()
{
    delete[] hobby;
}

Cow & Cow::operator=(const Cow &c) // overloaded assignment operator
{
    strcpy(name, c.name);
    char *temp = hobby;
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby);
    delete[] temp;
    weight = c.weight;
    return *this;
}

void Cow::ShowCow() const
{
    std::cout << "Name: " << name << '\n';
    std::cout << "Hobby: " << hobby << '\n';
    std::cout << "Weight: " << weight << "\n\n";
}

Client:

// main.cpp -- Exercising the Cow class (compile with cow.cpp)

#include "cow.h"
#include <iostream>

int main()
{
    using std::cout;
    using std::cin;

    Cow subject1; // default constructor
    Cow subject2("Maria", "Reading", 120); // non-default constructor
    Cow subject3("Lula", "Cinema", 135);
    subject1 = subject3; // overloaded assignment operator
    Cow subject4 = subject2; // copy constructor
    subject1.ShowCow();
    subject2.ShowCow();
    subject3.ShowCow();
    subject4.ShowCow();

    cin.get();
    return 0;
}

I was hiding some parts of the code to locate the possible problem and it seems the progam doesn’t like this two lines:

subject1 = subject3;
Cow subject4 = subject2

And in particular, in the overloaded assignment operator and the copy constructor, if I hide the delete[] temp line, the program doesn’t crash.

I’m total noob and probably is something stupid but I can’t see what I’m doing wrong in these definitions.

Any help?

  • 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-10T02:43:27+00:00Added an answer on June 10, 2026 at 2:43 am
    Cow::Cow(const Cow &c) // copy constructor
    {
        strcpy(name, c.name); // copies the value to the desired address
        char *temp = hobby; // stores the address of the memory previously allocated with new
        hobby = new char[strlen(c.hobby) + 1];
        strcpy(hobby, c.hobby); // copies the value to the new address
        delete[] temp; // deletes the previously new allocated memory
        weight = c.weight;
    }
    

    It’s the copy c-tor. There is no previously allocated members. this->hobby points to random garbage. So when you delete[] temp (i.e. this->hobby before the new assignment), you get UB.

    Cow & Cow::operator=(const Cow &c) // overloaded assignment operator
    {
        strcpy(name, c.name);
        char *temp = hobby;
        hobby = new char[strlen(c.hobby) + 1];
        strcpy(hobby, c.hobby);
        delete[] temp;
        hobby = name;
        weight = c.weight;
        return *this;
    }
    

    hobby = name is incorrect, as it causes a memory leak + destructor will try to delete an object that was not allocated with operator new[].

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

Sidebar

Related Questions

noob here. Having a doubt while doing an exercise from a book. The doubt
noob here and need help from the pros... i am doing a voice record
NOOb here. I've got a HTTP request that pulls all of the content from
c#/oop noob here. I am creating a datatable from the schema of a sql
Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__
Emacs noob here, For some reason, when I try to Open a existing C++
Rails noob here so I'm not sure what I'm doing wrong. We're replacing a
Groovy noob here, I'm working through my first Groovy book and it has example
noob here, I'm trying to transfer a file using rsync from windows to linux.
Noob here getting stuck and probably doing something stupid and would appreciate some guidance.

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.