Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我可以使用动态分配的jsoncpp吗?_C++_Jsoncpp - Fatal编程技术网

C++ 我可以使用动态分配的jsoncpp吗?

C++ 我可以使用动态分配的jsoncpp吗?,c++,jsoncpp,C++,Jsoncpp,我面临一些jsoncpp内存损坏的问题 当我在本地Json::Value变量中分配一些值时,有时它会获取错误的数据并导致崩溃 因此,我尝试使用动态分配生成Json::value变量,并更仔细地检查内存损坏 无论如何,我的问题是,我可以使用动态分配的jsoncpp吗?你认为它比以前安全吗 我很抱歉我的英语不好 谢谢大家! JsonCpp在您想要自己管理对树中的值的引用时,或者当您想要引用值的内部时,可能会变得不方便。 请参阅下面的代码,该代码描述了如何节省使用它的三种方法,以及显示一些常见陷阱的两

我面临一些jsoncpp内存损坏的问题

当我在本地Json::Value变量中分配一些值时,有时它会获取错误的数据并导致崩溃

因此,我尝试使用动态分配生成Json::value变量,并更仔细地检查内存损坏

无论如何,我的问题是,我可以使用动态分配的jsoncpp吗?你认为它比以前安全吗

我很抱歉我的英语不好


谢谢大家!

JsonCpp在您想要自己管理对树中的值的引用时,或者当您想要引用值的内部时,可能会变得不方便。 请参阅下面的代码,该代码描述了如何节省使用它的三种方法,以及显示一些常见陷阱的两个示例。希望能有点帮助

请注意,在处理“动态分配”时,通常智能指针非常方便,可以减少由于在正确的位置分配/删除对象时出现错误而导致内存泄漏或内存损坏的风险。例如,授予

Json::Value createJsonValue(){
Json::Value Json(“字符串值”);
return json;//确定-强制执行副本
}
Json::Value*createJsonValueReference(){
Json::Value*Json_dynamic=新的Json::Value(“字符串值”);
return json_dynamic;//确定-不强制执行副本,但将json值保留在堆中
}
std::shared_ptr createJsonValueSmartPointer(){
std::shared_ptr结果(新的Json::Value(“字符串值”);
return result;//确定-在堆上创建一个json::value并用一个共享的\u ptr对象包装它
}
Json::Value&referenceToLocalJson(){
Json::Value Json(“字符串值”);
return json;//不正常:返回对与局部变量'json'关联的堆栈内存的引用
}
const char*getJsonValueContent(){
Json::Value Json(“字符串值”);
return json.asCString();//critical:对将被删除的对象内部的引用。
}
int main()
{
Json::Value copied=createJsonValue();//将是一个副本;生命周期将持续到main的末尾
Json::Value*ptr=createJsonValueReference();//将是对堆上对象的引用;在调用'delete ref'之前一直有效`
std::shared_ptr smartptr=createJsonValueSmartPointer();//share_ptr对象,管理对堆上对象的引用;共享_ptr的生存期直到main结束;被引用对象的生存期直到指向它的最后一个共享_ptr被销毁
Json::Value&critical=referenceToLocalJson();//critical;将引用在“referenceToLocalJson”末尾已删除的对象
const char*content=getJsonValueContent();//临界:对将被删除的对象内部的引用。

可能无法。如果您发布一些示例代码,展示您在使用非动态分配时所做的工作,以及在使用动态分配时所做的工作,您将得到更好的答案。内存损坏的一点是,通过执行更多的分配,您可能会将损害转移到只会在执行i重要的演示/当您的老师运行它时。
Json::Value Json=new Json::Value(“字符串值”);
应该没有
new
?(也可能会提到C++11智能指针?@UnholySheep:对;相应地修改了答案。
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;
}