指针在函数调用后更改其地址

pointer is changing its address after a function call

本文关键字:地址 函数调用 指针      更新时间:2023-10-16

我对这部分代码有一个问题:

const float *dest = (float*)varPtr1.memoryPointer;
float temp; 
ReadFromMemory(temp);
.
.
continue the rest of treatment with 'dest' and 'temp'

一切正常,当到达第二行时,"dest"具有正确的值,但是,在调用函数ReadFromMemory((后,"dest"指针将其地址更改为 0x100000000,当然它的值变得错误(指向另一个地址(。

所以我的问题是,为什么"dest"在调用func后会改变其目标,尽管它已经超出了这个范围。

注意:温度是正确的

template<typename T>
void myClass:: ReadFromMemory(T &var, int var_pos)
{
if(cons64requested)
{
cons64requested = false;
}
else
{
if(var_pos==2)
{
const4NextInstruction = snd_param;
}
else
{
const4NextInstruction = fst_param;
}
}
memcpy(&var,&const4NextInstruction,0x8);
}

这个问题的解决方案是不传递 temp 作为引用,而是传递它的指针。 因此,函数声明如下所示:

void myClass:: ReadFromMemory(T *var, int var_pos)