在刷OJ的时候碰到了这个问题:
struct Node{
string str;
int count;
};
Node node[100]={'\0',0};
我当时需要把count的值初始化为0(但其实你不初始化它默认的就是0,但我开始不知道)
然后出现了这个错误:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
意思就是你的string指针指向了一个空值。
string类指针指向空值本来是很正常的,例如弄一个空指针:
string str=null;
但这里却不行,因为你是在结构体里默认的空指针,等于是在基类string的construct弄了个无效指针,导致运行中断,也就是RE。
本文探讨了在C++中使用结构体时,如何正确地对string成员进行默认初始化的问题。当直接在结构体定义中初始化string成员为默认状态时,可能会导致运行时错误。文章详细解释了这一现象的原因,并给出了正确的初始化方法。
1万+

被折叠的 条评论
为什么被折叠?



