我可以将 jsoncpp 与动态分配一起使用吗?
Can I use jsoncpp with dynamic allocation?
我遇到了一些内存损坏的jsoncpp问题。
当我在本地 Json::Value 变量中分配一些值时,有时它会得到错误的数据并导致崩溃。
所以我正在尝试使用动态分配使 Json::value 变量并更仔细地检查内存损坏。
无论如何,我的问题是,我可以将jsoncpp与动态分配一起使用吗?你认为它比以前安全吗?
我很抱歉我缺乏英语。
谢谢!
当您想要自行管理对树中值的引用时,或者当您想要引用值的内部时,JsonCpp 可能会变得不方便。请参阅以下代码,其中介绍了如何节省使用的三种方法,以及显示一些常见陷阱的两个示例。希望它有所帮助。
请注意,在处理"动态分配"时,智能指针通常非常方便,可以降低由于在正确点分配/删除对象时的错误而导致内存泄漏或内存损坏的风险。例如,授予shared_ptr。
Json::Value createJsonValue() {
Json::Value json("a string value");
return json; // OK - enforces a copy
}
Json::Value *createJsonValueReference() {
Json::Value *json_dynamic = new Json::Value("a string value");
return json_dynamic; // OK - does not enforce a copy but keeps json value in heap
}
std::shared_ptr<Json::Value> createJsonValueSmartPointer() {
std::shared_ptr<Json::Value> result(new Json::Value("a string value"));
return result; // OK - creates a json::value on the heap and wraps it by a shared_ptr object
}
Json::Value &referenceToLocalJson() {
Json::Value json("a string value");
return json; // Not OK: Reference to stack memory associated with local variable `json` returned
}
const char* getJsonValueContent() {
Json::Value json("a string value");
return json.asCString(); // critical: reference to internals of object that will be deleted.
}
int main()
{
Json::Value copied = createJsonValue(); // will be a copy; lifetime is until end of main
Json::Value *ptr = createJsonValueReference(); // will be a reference to an object on the heap; lifetime until you call `delete ref`
std::shared_ptr<Json::Value> smartptr = createJsonValueSmartPointer(); // share_ptr object, managing a reference to an object on the heap; lifetime of shared_ptr until end of main; lifetime of referenced object until the last shared_ptr pointing to it is destroyed
Json::Value &critical = referenceToLocalJson(); // Critical; will refer to an object that has already been deleted at the end of "referenceToLocalJson"
const char* content = getJsonValueContent(); // critical: reference to internals of object that will be deleted.
cout << "copied:" << copied << std::endl;
cout << "reference:" << *ptr << std::endl;
cout << "smartptr:" << *smartptr << std::endl;
// cout << "critical:" << critical << std::endl; // undefined outcome
// cout << "content:" << content << std::endl; // undefined outcome
delete ptr; // OK - will free object referred to by ptr
// smartptr will be deleted together with the json value it refers to at the end of this function; no "explicit" delete
return 0;
}
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 在c++中使用动态分配的问题
- 使用递归模板动态分配的多维数组
- 对具有动态分配的内存和析构函数的类对象的引用
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 访问动态分配列表中的元素
- 为什么 std::equal_to会导致动态分配?
- 调用析构函数以释放动态分配的内存
- 动态分配Q_Property变量
- 在 C++ 中搜索动态分配的数组中的出现次数
- 动态分配的聊天数组打印缺失的数据和空
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 使用动态分配将 char* 复制到另一个字符**
- 使用指针在存在特征库的情况下动态分配 c++ 中的矩阵
- 二维阵列的动态分配
- 0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误
- 我可以将 jsoncpp 与动态分配一起使用吗?
- 如何将复制构造函数与动态分配一起使用
- 将无序映射与动态分配的用户定义类一起使用