Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将结构复制(使用赋值)到导致seg故障的并集内的结构_C++_Struct_Memcpy_Unions - Fatal编程技术网

C++ 将结构复制(使用赋值)到导致seg故障的并集内的结构

C++ 将结构复制(使用赋值)到导致seg故障的并集内的结构,c++,struct,memcpy,unions,C++,Struct,Memcpy,Unions,我编写了以下代码: #include <iostream> #include <string> #include <cstring> struct bar { std::string s3; std::string s4; }Bar; union foo { char * s1; char * s2; bar b1; foo(){}; ~foo(){}; }Foo; int main () { foo f1;

我编写了以下代码:

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

struct bar
{
  std::string s3;
  std::string s4;
}Bar;

union foo
{
  char * s1;
  char * s2;
  bar    b1;

  foo(){};
  ~foo(){};
}Foo;


int main ()
{
  foo f1;
  bar b2;

  std::string temp("s3");
  b2.s3 = temp;
  b2.s4 = temp;

  //f1.b1 = b2;                           //-- This Fails (Seg faults)

  /*
    #0  0x00002b9fede74d25 in std::string::_Rep::_M_dispose(std::allocator<char> const&) [clone .part.12] ()
        from /usr/local/lib64/libstdc++.so.6
    #1  0x00002b9fede75f09 in std::string::assign(std::string const&) () from /usr/local/lib64/libstdc++.so.6
    #2  0x0000000000400ed1 in bar::operator= (this=0x7fff3f20ece0) at un.cpp:5
    #3  0x0000000000400cdb in main () at un.cpp:31
  */

  memcpy( &f1.b1, &b2, sizeof(b2) );  //-- This Works 

  std::cout << f1.b1.s3 << " " << f1.b1.s4 << std::endl;
  return 0;
} 
#包括
#包括
#包括
结构条
{
std::字符串s3;
std::字符串s4;
}酒吧;
联富
{
char*s1;
char*s2;
条b1;
foo(){};
~foo(){};
}傅;
int main()
{
富f1;
钢筋b2;
标准:字符串温度(“s3”);
b2.s3=温度;
b2.s4=温度;
//f1.b1=b2;/--此故障(Seg故障)
/*
#std::string::_Rep::_M_dispose(std::allocator const&)[clone.part.12]()
from/usr/local/lib64/libstdc++.so.6
#1 0x00002b9fede75f09位于/usr/local/lib64/libstdc++.so.6的std::string::assign(std::string const&)()中
#在un.cpp:5处,条形图中的2 0x0000000000400ed1::运算符=(此=0x7fff3f20ece0)
#un.cpp:31处的main()中有3 0x0000000000400cdb
*/
memcpy(&f1.b1,&b2,sizeof(b2));/--这很有效

std::cout
std::string
有一个非平凡的构造函数,用于初始化其内部成员。因此,
struct bar
不是POD结构

联合体只支持POD(在C++11中这是宽松的)。编译器无法决定调用联合体成员的哪个构造函数。请想象以下情况:

unition MyUnion {
  std::string s;
  std::vector v;
};
它应该使用vector或string的构造函数来初始化对象吗


因此,在您的情况下,当您将字符串分配给联合的字符串时,内部数据不会初始化,这会导致随机错误。

您不能使用memcpy复制对象或包含对象的结构,因为它们不会正确初始化。字符串有指向字符数组的指针,如果两个字符串可以共享同一个指针,则必须进行某种垃圾收集(通常是引用计数器)。如果执行f1.b1=b2,编译器将生成代码以正确初始化字符串。

union foo
无法初始化bar对象(它如何知道调用哪个成员的初始值设定项?)因此无法初始化
std::string
s。如果要使用foo内部的条,则需要手动初始化它,如下所示

new (&f1.b1) bar; // Placement new
f1.b1 = b2;
// And later in code you'll have to manually destruct the bar, because
//   foo doesn't know to destruct the bar either...
f1.b1.~bar();
或者,您可以尝试自己将此功能滚动到联合的构造函数和析构函数中

foo() : b1() {}
// Or you construct like this, which you might need to for a non-trivial union...
// foo() { new (&b1) bar; }  // Placement new.
~foo() { b1.~bar(); }

请注意,复制也需要特殊处理。

@Wilding:代码不编译。是的,它编译了。请再次检查。我这样编译:
g++-Wall-g-std=c++11 un.cpp-o u
我在VS2013上编译了它。请粘贴错误。错误2错误C2039:'b1':不是'foo'main的成员。cpp 41 1错误3错误C2039:'b1':不是“foo”main的成员。cpp 43 1错误4错误C2228:left of“.s3”必须具有类/结构/联合main。cpp 43 1错误5错误C2228:left of“.s4”必须具有类/结构/联合main。cpp 43 1同样,我使用的是
C++11
这是不可能的。即使在C++11中,它也只能在简单的构造函数上工作。请看一下Bjarne Stroups C++11关于这个问题的常见问题解答:但是Maurice,我不能做
f1.b1=b2
,因为std::string有一个非平凡的构造函数。我在这里有什么选择?我认为你应该重新考虑你的数据结构。有一点是肯定的,你只能初始化联合的第一个字段。如果b1是Foo的第一个字段,你可以如下初始化f1:Foo f1={Bar()};Wildling,你可以使用C++11,然后非常、非常、非常、非常仔细地阅读联合的扩展。但是如果你问stackoverflow,你可能不应该使用它。@gnasher729也许吧,但是对于联合,我总是觉得我用得不够。你解决了它。尽管其他答案对我帮助很大,但这个答案是正确的问题的实际答案。我的代码是通过在
f1.b1=b2;
之前简单地添加
new(&f1.b1)bar;
修复的。请注意,仅添加新的位置会泄漏。bar对象(以及它的字符串)除非您手动执行,否则将永远不会进行析构函数。是的,我已经处理好了。将分配和取消分配添加到构造函数和析构函数中。实际上…执行析构函数可能不好。如果将联合用作字符指针,则会使字符串数据无效,这可能会破坏现在已初始化的字符串,需要销毁d字符串数据。老实说,如果没有更多关于你到底想要实现什么的细节,就不可能说如何处理你的工会。工会通常都是非常棘手的对象,有着非常特殊的情境需求。