当前位置:Gxlcms > 数据库问题 > 消除警告 writing to an object of type ‘class XXOO’ with no trivial copy-assignment; use copy-assignment or copy-initialization instead [-Wclass-memaccess]

消除警告 writing to an object of type ‘class XXOO’ with no trivial copy-assignment; use copy-assignment or copy-initialization instead [-Wclass-memaccess]

时间:2021-07-01 10:21:17 帮助过:28人阅读

#include <cstring> 2 3 struct A 4 { 5 A(int size) : size_(size), data_(new int[size]) {} 6 ~A() { delete [] data_; } 7 8 // The copy constructor and the copy assignment operator need 9 // to be implemented for the class too. They have been omitted 10 // to keep the code here minimal. 11 12 int size_; 13 int* data_; 14 }; 15 16 int main() 17 { 18 A a1(10); 19 A a2(20); 20 std::memcpy(&a1, &a2, sizeof(A)); 21 22 // When we return from the function, the original data_ of a1 23 // is a memory leak. The data_ of a2 is deleted twice. 24 25 return 0; 26 }

执行完拷贝时,此时a1.data_已经被a2.data_所覆盖,a1实例化时申请的int数组已经没有指针指向它(第五行代码)。程序退出时。这部分内存溢出。

解决方案,目前还没有决定用哪个:

1.重写类,使用拷贝构造函数,弃用memcpy。

优点,就应该这么办,缺点,工程量有点大,老代码,没有敢动。

2.MakeFile中添加[-Wclass-memaccess]将警告屏蔽。

优点,修改的时间快。

缺点,不讲武德,欺骗客户。

3.强制类型转换

memcpy ( &upoData[i] , &pData , sizeof ( UData ) ) ;---->memcpy ( (void*)&upoData[i] ,  (void*)&pData , sizeof ( UData ) ) ;

优点,修改的时间快。相比第二种看着舒服点。

缺点,也不太讲武德,有点欺骗客户和编译器感情。

目前调查出三种修改方案。具体采用哪种定下来再补充。

参考链接:

c++ - What uses are there for "placement new"? - Stack Overflow

c++ - Using std::memcpy to object of non-trivially copyable type - Stack Overflow

c++ - Avoid `-Wclass-memaccess` on memcpy of a POD type w/copy disabled - Stack Overflow

 

消除警告 writing to an object of type ‘class XXOO’ with no trivial copy-assignment; use copy-assignment or copy-initialization instead [-Wclass-memaccess]

标签:版本   span   nes   code   ase   cstring   rgba   with   指针   

人气教程排行