4

I am very curious why is copy constructor so important for the dynamic allocation of my own defined class.

I am implementing the low-level c-string class with dynamic allocations and here is a quick view of my class

class String
{
private:
    char * buf;
    bool inBounds( int i )
    {
        return i >= 0 && i < strlen(buf);
    }
    static int strlen(const char *src)
    {
        int count = 0;
        while (*(src+count))
            ++count;
        return count;
    }
    static char *strcpy(char *dest, const char *src)
    {
        char *p = dest;
        while( (*p++ = *src++));
        return dest;
    }
    static char* strdup(const char *src)
    {
        char * res = new_char_array(strlen(src)+1);
        strcpy(res,src);
        return res;
    }
    static char * new_char_array(int n_bytes)
    {
        return new char[n_bytes];
    }
    static void delete_char_array( char* p)
    {
        delete[] p;
    }

public:
    /// Both constructors should construct
    /// this String from the parameter s
    String( const char * s = "")
    {
        buf = strdup(s);
    }
    String( String & s)
    {
        buf = strdup(s.buf);
    }
    void reverse()
    {
    }
    void print( ostream & out )
    {
        out << buf;
    }
    ~String()
    {
        delete_char_array(buf);
    }
};
ostream & operator << ( ostream & out, String str )
{
    str.print(out);
    return out;
}

I know the part of strdup() function is not really correct but I am just doing some tests.

My problem is if I do not have the copy constructor and my main() is

int main()
{
    String b("abc");
    String a(b);
    cout << b << endl;
    return 0;
}

The compiler will tell me double free or corruption (fasttop) and I find some answers about this question and see the Big three rules.

Can you guys tell me why my code works without any errors if I have the copy constructor and what the error of double free or corruption (fasttop) means?

Ruidongd
  • 41
  • 5
  • it will just copy the addresses, not duplicate the objects. You have to redefine the copy constructor to make the duplication. – Pierre Emmanuel Lallemant Feb 03 '16 at 00:13
  • It's not a good idea to give your member functions the same name as C standard library functions, because they may be defined as preprocessor macros – M.M Feb 03 '16 at 00:23
  • @M.M This is my homework, like helping me to understand how the c++ works. I will notice this in further when I get job. – Ruidongd Feb 03 '16 at 00:42

1 Answers1

6

If you don't define a copy constructor, the compiler will insert one for you. This default copy constructor will simply copy all the data members, so both instances of String will point to the same area of memory. The buf variable will hold the same value in each instance.

Therefore when the instances go out of scope and are destroyed, they will both attempt to release the same area of memory, and cause an error.

Sod Almighty
  • 1,768
  • 1
  • 16
  • 29