Ere*_*cie 0 c++ struct pointers
我试图将一个空的struct指针传入一个名为"initialize"的函数作为输出参数,然后在函数完成后我可以检索struct指针指向的对象.一些意想不到的行为发生,我不太明白.
以下是我的代码:
static void initialize(complex_struct*& infoPtr)
{
complex_struct myInfo;
infoPtr = &myInfo;
// some other codes that modify the contents of myInfo
infoPtr->input_components = 3;
}
static void myfunction()
{
complex_struct* infoPtr = nullptr;
initialize(infoPtr);
std::cout << infoPtr->input_components << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这个输出是一个任意的大整数.但我期待打印整数3.
限制:我无法修改complex_struct; 它是一个需要使用的third_party结构.另外,我需要将initialize()作为一个单独的函数的原因是我正在对complex_struct的内存分配进行性能测量.因此,将initialize()中的第一行代码移动到myfunction()并不是一个理想的解决方案.
您正在尝试使用指向其范围函数之外的局部变量的指针.在您的示例中,退出函数后将删除myInfo实例内部initialize(),并且您记住的地址将指向随机垃圾内存.您永远不应该使用指向其作用域之外的局部变量的指针.
如何解决这个问题?最简单的方法是抛弃指针,而不是通过非const引用传递你的结构.代码如下所示:
void initialize(complex_struct& info)
{
// some other codes that modify the contents of myInfo
info.input_components = 3;
}
void myfunction()
{
complex_struct info;
initialize(info);
std::cout << info.input_components << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
建议的代码中有一个微妙的缺陷:有效的信息被初始化两次.第一次创建实例时(complex_struct info)第二次在initialize()函数内部.它在这个例子中没有任何明显的效果(info在堆栈上分配,我不认为它有任何非平凡的构造函数)但在其他设置中可能会有更大的问题.在这种情况下初始化它的最好方法是从初始化函数返回结构,并依靠copy-elision来优化掉所有副本.插图代码:
complex_struct initialize()
{
complex_struct info;
// some other codes that modify the contents of myInfo
info.input_components = 3;
return info;
}
void myfunction()
{
complex_struct info = initialize();
std::cout << info.input_components << std::endl;
}
Run Code Online (Sandbox Code Playgroud)