结构的std :: string成员的EXC_BAD_ACCESS错误 [英] EXC_BAD_ACCESS error for std::string member of a structure
问题描述
在访问类型为std :: string的结构成员时,错误 Bus Error:10 弹出.代码如下.
On accessing a struct member of type std::string, the error Bus Error: 10 popped up. Code is as following.
#include <iostream>
#include <string>
struct KeyValuePair {
std::string key;
std::string value;
};
struct KeyValuePair *temp = (struct KeyValuePair *) malloc(sizeof(struct KeyValuePair));
int main(void) {
temp->value = "|";
temp->value += "someval|";
std::cout << temp->value << std::endl;
return 0;
}
在代码上运行gdb时,在 temp-> value ="|" 行显示以下内容.
Running gdb on the code shows the following at the line temp->value = "|".
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff8d99e524
0x00007fff898dc7ca in std::string::_M_mutate ()
从以上消息中,我所了解的就是我的代码正在尝试访问无效/未经授权的内存区域.
From above message all I understand is that my code is trying to access an invalid/unauthorized memory area.
我的问题:尽管我已经使用malloc来获取全局变量 temp 的内存区域,但是为什么我无法访问它.我想念的是什么.请帮忙.
My question: Though I have used malloc to obtain a memory area for the global variable temp, why am I unable to access it. What am I missing. Please help.
推荐答案
C和C ++之间有区别:
There is a difference between C and C++:
- 在C语言中,
struct
的实例没有固有的不变,它只是位于内存中 - 在C ++中,
struct
或class
的实例具有一组不变量,它们由构造函数建立并由公众维护界面在整个生命周期中
- in C, an instance of a
struct
has no inherent invariant, it just sits in memory - in C++, an instance of a
struct
orclass
has a set of invariants that are established by the constructor and maintained by the public interface throughout its lifetime
显示在这里:
-
malloc
是C构造,只会保留一些原始内存 -
new
是C ++构造,不仅会保留一些原始内存,还会调用适当的构造函数,从而建立实例不变式(如果有的话)
malloc
, being a C construct, will just reserve some raw memorynew
, being a C++ construct, will not only reserve some raw memory, but also call the appropriate constructor and thus establish the instance invariants (if any)
注意:如果将 new
与内置类型(例如 int
)一起使用,则 new int
实际上不会初始化任何内容.
Note: if using new
with built-in types such as int
, new int
does not actually initialize anything...
因此,在访问temp->value
时,您正在访问未初始化的内存.这是未定义的行为(可能会发生任何事情),并且在您的情况下,程序会遵循一些野指针并落在禁止访问的内存区域上.
Therefore, when accessing temp->value
, you are accessing uninitialized memory. This is Undefined Behavior (anything may happen), and, in your case, the program follows some wild pointer and lands on a memory zone it is forbidden to access.
所以,现在就暂时忘记那些C-ism:常规的C ++构造将确保为您适当地调用构造函数.
So, just forget about those C-isms for now: regular C++ constructs will ensure the constructor is appropriately called for you.
KeyValuePair temp;
int main() {
// ...
}
或者,如果您确实需要动态分配的值(代表什么?):
Or, if you really need a dynamically allocated value (what for ?):
KeyValuePair* temp = new KeyValuePair();
int main() {
// ...
}
但是随后您必须考虑在某个时间点在 temp
上调用 delete
,一次,这要复杂得多.没有智能指针,这将是一场失败的游戏.
But then you have to think about calling delete
on temp
at some point, only once, which is much more complicated. Without a smart pointer, it is a losing game.
这篇关于结构的std :: string成员的EXC_BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!