Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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++; 我很难解决运行时问题C++,使用Visual Studio 2010,它编译好./P>_C++_Function_Reference_Constants - Fatal编程技术网

常量字符*&;作为函数参数C++; 我很难解决运行时问题C++,使用Visual Studio 2010,它编译好./P>

常量字符*&;作为函数参数C++; 我很难解决运行时问题C++,使用Visual Studio 2010,它编译好./P>,c++,function,reference,constants,C++,Function,Reference,Constants,我在API(来自另一个DLL)的类中有一个函数,其签名如下: ReturnCode GetValue(const char* &value) const; 我称之为: ReturnCode Test::getStuff(const char* &value) { ValueGetter valueGetter = myClass.GetValueGetter(); ReturnCode returnCode = valueGetter.GetValue(valu

我在API(来自另一个DLL)的类中有一个函数,其签名如下:

ReturnCode GetValue(const char* &value) const;
我称之为:

ReturnCode Test::getStuff(const char* &value)
{
    ValueGetter valueGetter = myClass.GetValueGetter();
    ReturnCode returnCode = valueGetter.GetValue(value);
    return returnCode;
}
这就叫使用

const char* tempStr;
getStuff(tempStr);

我看到的是,在调用valueGetter.GetValue(value)之后,在该行上正确设置了值。但是,只要我通过再次单步执行超出函数的范围,值就会变得乱七八糟。我做错了什么,如何正确地做?希望不必分配内存、复制值,我以后会删除它们。

看起来它返回一个指向超出范围的变量的指针。 要么是
ValueGetter::GetValue
的本地变量,要么是
ValueGetter
的数据成员(请注意,
ValueGetter
返回时超出范围)

在后一种情况下,这应该起作用:

ReturnCode Test::getStuff(std::string &value)
{
    ValueGetter valueGetter = myClass.GetValueGetter();
    const char* tempStr;
    ReturnCode returnCode = valueGetter.GetValue(tempStr);
    value = tempStr;
    return returnCode;
}


std::string tempStr;
getStuff(tempStr);

这里似乎没有什么问题。您如何调用
getStuff
?您在valueGetter.GetValue中为值分配了什么?这似乎是一个局部值超出范围并被破坏的情况(您分配给值的那个)。此外,如果在发布模式下运行程序,可能会产生优化效果。
值变为乱码
值本身是否发生变化,或者该值指向的内存变为乱码?我正在调试中运行,该值启动0xCCCCCC in函数。调用0x0000000002123290“测试”后。只要一步移到}大括号,指针就相同,但值变为奇怪的符号/空格/随机。我更新了这个例子,我忘了我也有一个临时赋值,而不是类中的成员变量。我想知道是不是因为它超出了范围,它所指的const char*指针的内存也超出了范围……但是有没有办法不复制/分配内存就完成我正在做的事情。你还能显示你指向哪个字符串的
值吗?您能在项目设置中验证您使用的是哪种运行时吗?我想这会起作用,尽管它迫使我进入std库。