0

I have created a basic class object with setter and getters based on the text book. The code work fine but when I attempt to change the data in name from Square to Circle, I get error : double free or corruption (fasttop).

Can someone enlighten me what is happening here? Is this due to some kind of memory allocation error?

Below is a reproducible code:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Shape
{
    protected:
        string name;
        bool contains = false;
        
    public:
        Shape(string name, bool contains)
        {
            setName(name);
            setContains(contains);
        }
        
        string getName();
        bool getContains();
        string setName(string name);
        bool setContains(bool contains);
};

string Shape :: setName(string inputName)
{
    name = inputName;
}

bool Shape :: setContains(bool inputContains)
{
    contains = inputContains;
}

string Shape :: getName()
{
    return name;
}

bool Shape :: getContains()
{
    return contains;
}

int main()
{
    Shape object1("Square", true);
    cout << "Testing" << endl;
    cout << object1.getName() << endl;
    cout << object1.getContains() << endl;

    object1.setName("Circle");
    cout << object1.getName() << endl;
    return 0;
}

Edit:

Interesting, when running the code in OnlineGDB without object1.setName("Circle"); and cout << object1.getName() << endl; will report a segmentation fault error. What is the way to tackle this issue?

user3118602
  • 553
  • 5
  • 19
  • 2
    Your setName and setContains function promise to return something but don't. This is undefined behavior. Isn't your compiler throwing warnings about these lines? – Botje Jul 28 '20 at 11:38
  • When I compile it, it didn't say any error. I am using Ubuntu terminal to do this. – user3118602 Jul 28 '20 at 11:38
  • 1
    Set `-Wall -Wextra -Werror -pedantic` to gcc or clang. – Quimby Jul 28 '20 at 11:39
  • Ah. Add `-Wall -Werror` to your compiler flags. – Botje Jul 28 '20 at 11:39
  • Sorry, may I know the purpose of the command? As this is a virtual environment provided by the school, they specifically told us not to mess around with any of the setup in the Ubuntu. – user3118602 Jul 28 '20 at 11:41
  • If I run this in onlinegdb, the same error appear too if that helps anyone to explain to me what is going on. – user3118602 Jul 28 '20 at 11:42
  • You can use valgrind to better analyze and identify the cause of this issue. – buchipper Jul 28 '20 at 11:43
  • The root cause is still the same: you promised to return something and did not, the program is crashing as a result. Those flags tell the compiler to check your code for common programming errors and tell you about them. Add them to the the line where you call `g++` or `clang++`. – Botje Jul 28 '20 at 11:48
  • See https://stackoverflow.com/questions/57842756 – n. m. could be an AI Jul 28 '20 at 11:49

1 Answers1

0

Your setName and setContains functions don't have any return statements while their return types are not void. Therefore, executing these functions invokes undefined behavior.

N3337 6.6.3 The return statement says:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

To fix this issue, you should do one of:

  • Change return types of the functions setName and setContains to void.
  • Add return statements with proper return value to the functions.
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Hi Mike. Thank you, I don't really understand the second point. When you mentioned to add `return` statement with proper return value to functions, do you mean to say that I need to put the datatype in setName for the `name` and `contains` variables? – user3118602 Jul 28 '20 at 13:19
  • No. The variables `name` and `coutains` already have the datatype in the class declaration, so you don't need to give them the datatype again. – MikeCAT Jul 28 '20 at 13:21
  • Thanks Mike, took me awhile but I finally understood! – user3118602 Jul 28 '20 at 13:25