结构体的std::string成员的EXC_BAD_ACCESS错误
EXC_BAD_ACCESS error for std::string member of a structure
访问std::string类型的struct成员时,弹出错误Bus error: 10。代码如下:
#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 = "|"行显示以下内容。
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff8d99e524
0x00007fff898dc7ca in std::string::_M_mutate ()
从上面的消息中,我所理解的是我的代码试图访问一个无效/未经授权的内存区域。
我的问题:虽然我已经使用malloc来获取全局变量temp的内存区域,为什么我无法访问它。我错过了什么?请帮助。
您需要使用new
而不是malloc
来确保构建了std::string
对象。
KeyValuePair *temp = new KeyValuePair;
当需要销毁结构体时,使用delete
。
delete temp;
作为一般的经验法则,在c++中编码时不应该使用malloc
。使用new
代替。
也就是说,对于您的简单示例,似乎没有真正需要动态分配。您可以像这样避免动态分配:
KeyValuePair temp;
您在c++中使用C结构。完全消除malloc
:
KeyValuePair temp; // Yes, that simple.
int main( ) {
temp.value = "|";
C和c++的区别:
- 在C语言中,
- 在c++中,
struct
或class
的实例具有一组不变量,这些不变量由构造函数建立并在其整个生命周期中由公共接口维护
struct
实例没有固有的不变量,它只是位于内存中显示在这里:
-
malloc
,作为一个C结构,将只保留一些原始内存 -
new
作为一个c++构造,不仅会保留一些原始内存,而且还会调用适当的构造函数,从而建立实例不变量(如果有的话)
注意:如果将new
与内置类型(如int
)一起使用,new int
实际上不会初始化任何东西…
temp->value
时,您访问的是未初始化的内存。这是未定义的行为(任何事情都可能发生),在您的情况下,程序跟随某个野指针并落在禁止访问的内存区域。
所以,现在就忘掉那些C-isms:常规的c++构造将确保构造函数被适当地调用。
KeyValuePair temp;
int main() {
// ...
}
或者,如果您确实需要动态分配的值(为什么?):
KeyValuePair* temp = new KeyValuePair();
int main() {
// ...
}
但是你必须考虑在某个时候在temp
上调用delete
,只有一次,这要复杂得多。没有智能指针,这是一场失败的游戏。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 新的 C++14 位分隔符处"Bad Character" Eclipse Oxygen 错误
- 解密文件AES_256_CBC返回"bad decrypt"错误
- OpenCV C++神经网络 predict() 函数抛出"Bad argument"错误
- cvLogPolar 导致 opencv 错误"Bad flag (parameter or structure field)"
- 生成文件'fdopen: Bad file descriptor'错误
- 来自 MSDN C++ 示例的 LDAP "The search filter is bad."错误
- 'Pre-mature EOF'和'Bad Character'错误
- 错误:在 CComVariant 中"bad variable type":循环访问 CComSafeArray 时复制
- fwrite 失败(返回 0,错误给出 "Bad address")
- Xcode4:再次"bad codegen, pointer diff"链接器错误