[英]How to assign value of a string variable to a string variable of a structure in c++?
I am trying to assign the value of a string variable to another string variable of a structure. 我正在尝试将字符串变量的值分配给结构的另一个字符串变量。 But gdb gives an runtime error. 但是gdb给出了运行时错误。 The error is as follows: Program received signal SIGSEGV, Segmentation fault. 错误如下:程序接收到信号SIGSEGV,分段错误。 0xb7f7c8f8 in std::string::assign(std::string const&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6 来自/usr/lib/i386-linux-gnu/libstdc++.so.6的std :: string :: assign(std :: string const&)()中的0xb7f7c8f8
My C++ program is: 我的C ++程序是:
#include<iostream>
#include<stdlib.h>
#include<string>
typedef long unsigned int LUI;
using namespace std;
struct graph {
string string_node;
LUI node;
struct graph *link;
};
struct graph *abc[30];
struct graph *t;
string x;
int main() {
t = (struct graph *) malloc(sizeof(struct graph *));
x = "abc";
t->string_node = x;
t->link = NULL;
abc[0] = t;
cout << "Value is " << abc[0]->string_node << endl;
cout << "end";
return 0;
}
Please help me to store the value of x into t->string_node. 请帮助我将x的值存储到t-> string_node中。 Thanks in advance.. 提前致谢..
t = (struct graph *) malloc(sizeof(struct graph *));
graph
is a class. graph
是一类。 It contains C++ classes, specifically it contains a std::string
. 它包含C ++类,特别是它包含std::string
。
All C++ classes must be constructed in dynamic scope using the new
operator. 必须使用new
运算符在动态范围内构造所有C ++类。 They cannot be constructed with the C library function malloc()
, which knows absolutely nothing, whatsoever, about C++ classes. 它们不能使用C库函数malloc()
构造,该函数对C ++类一无所知。 Doing so results in undefined behavior (not to mention that your malloc-ed size is wrong, anyway). 这样做会导致未定义的行为(更不用说您的malloc-ed大小是错误的)。
Now that you're writing C++ code, you need to completely forget that malloc()
, realloc()
, and free()
ever existed, and always use new
and delete
. 现在,您正在编写C ++代码,您需要完全忘记malloc()
, realloc()
和free()
曾经存在,并始终使用new
和delete
。
Your problem is that you're allocating a struct
with malloc
, but that struct
does not have only POD (plain old data) members: it has a std::string
member, and std::string
objects expect to be constructed . 您的问题是您正在使用malloc
分配一个struct
,但是该struct
不仅仅具有POD(普通旧数据)成员:它具有std::string
成员,并且std::string
对象应该被构造 。 Simply allocating memory for it with malloc
will not invoke the std::string
constructor, and consequently attempting to interact with that std::string
later will result in undefined behavior since that object is in a bad state. 简单地使用malloc
为它分配内存将不会调用std::string
构造函数,因此以后尝试与该std::string
交互将导致未定义行为,因为该对象处于错误状态。
You instead should use new
to allocate your struct
, and that will properly allocate memory and invoke the default constructor for each member. 相反,您应该使用new
来分配struct
,这将正确分配内存并为每个成员调用默认构造函数。 (Conversely, you should release that memory with delete
instead of free
to properly invoke the destructor of each member.) (相反,你应该释放与记忆delete
,而不是free
正确调用每个成员的析构函数 )。
Alternatively, it is possible to use "placement new
" to construct an object in memory you've already allocated, but this is not something that you should normally need to do. 另外,也可以使用“ placement new
”在已分配的内存中构造一个对象,但这通常不是您需要做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.