gcc8 is throwing compilation error due to non-trivially copyable type

819 views Asked by At
class mapInfo
{
public:
    mapInfo();
    ~mapInfo();  
public:
    int dataType_m;

private:
    int *frequency;
};

//constructor is defined here.

    mapInfo::mapInfo() :
    dataType_m(0),
    frequency(NULL)
{
}  

//destructor is defined here

mapInfo::~mapInfo()
{
    free(frequency);
    frequency = NULL;
}

Result_t Maps::add(mapInfo &mapInfo_r)
{
    if (maps_mp == NULL)
    {
        numMaps_m = 1;
        maps_mp = (mapInfo *) calloc(1, sizeof(mapInfo));
    }
    else
    {
        numMaps_m++;
        maps_mp = (mapInfo *) realloc(maps_mp, numMaps_m*sizeof(mapInfo));
    }
    maps_mp[numMaps_m-1] = mapInfo_r; // Default copy constructor
    return 1;
}

While compiling with gcc8, getting the following compilation error. It looks like defining the destructor like above giving the compilation error for gcc8.

How to resolve this?

 error: 'void* realloc(void*, size_t)' moving an object of non-trivially copyable type 'class xyyz::mapInfo'; use 'new' and 'delete' instead [-Werror=class-memaccess]. 
2

There are 2 answers

0
Konrad Rudolph On

That’s simply not proper C++. Rewrite your code as follows (I’m guessing here with regards to the type of frequency, but definitely don’t use free on it):

#include <vector>

class map_info
{
public:
    map_info();

private:
    int data_type;
    std::vector<int> frequency;
};
std::vector<map_info> maps_mp;

map_info::map_info() : data_type(0), frequency() {}

// …

void maps::add(map_info& map_info)
{
    maps_mp.push_back(map_info);
}

0
David Schwartz On
    maps_mp = (mapInfo *) realloc(maps_mp, numMaps_m*sizeof(mapInfo));

This is not sensible. You can't just move an object from one aree of memory to another if that object is non-trivial.

For example, consider a string object that keeps a pointer to the string. It could look like this:

class MyString
{
    char* inner_ptr;
    char buf[64];

    ...
};

And it might have a constructor like this:

MyString::MyString (const char* j)
{
     if (strlen(j) < 64)
         inner_ptr = buf;
     else
         inner_ptr = malloc (strlen(j) + 1);
     strcpy(inner_ptr, j);
}

And a destructor like this:

MyString::~MyString()
{
     if (buf != inner_ptr)
         free (inner_ptr);
}

Now, think about what happens if you call relloc on an array of these. The short strings will still have their inner_ptrs pointing to the old object's buffer, which you just deallocated.

The error message explains this issue reasonable well. It is simply not legal to use realloc to move a non-trivial object. You have to construct a new object because the object needs a chance to handle the change in its address.