常量变量正在更改,但不在内存中 首先,我在XCODC++中工作时,我得到了错误。我将指针变量声明为常量,并设置一次值。然后,在代码中,循环运行3-5次,第一次是正确的,但根据变量的数量,坐标值更改为接近0(即4.59163e-41)。我知道内存地址不会改变,只是它所持有的值会改变。我也在处理大量的数据,我指的是2000到20000个坐标。我相信这是我造成的,无论是大问题还是小问题,所以任何帮助都将不胜感激。下面是更改[not so]常量变量的一些代码:

常量变量正在更改,但不在内存中 首先,我在XCODC++中工作时,我得到了错误。我将指针变量声明为常量,并设置一次值。然后,在代码中,循环运行3-5次,第一次是正确的,但根据变量的数量,坐标值更改为接近0(即4.59163e-41)。我知道内存地址不会改变,只是它所持有的值会改变。我也在处理大量的数据,我指的是2000到20000个坐标。我相信这是我造成的,无论是大问题还是小问题,所以任何帮助都将不胜感激。下面是更改[not so]常量变量的一些代码:,c++,xcode,pointers,constants,C++,Xcode,Pointers,Constants,我在头文件中将它们声明为: const float* m_xPointValues; const float* m_yPointValues; 然后在.cpp文件中,我在以下函数中设置它们: void ccGraphDisplay::setPointValues(float* xPointValues, float* yPointValues, unsigned numberOfPoints) { assert(xPointValues); assert(yPointValue

我在头文件中将它们声明为:

const float* m_xPointValues;
const float* m_yPointValues;
然后在.cpp文件中,我在以下函数中设置它们:

void ccGraphDisplay::setPointValues(float* xPointValues, float* yPointValues, unsigned numberOfPoints)
{
    assert(xPointValues);
    assert(yPointValues);

    m_xPointValues = xPointValues;
    m_yPointValues = yPointValues;
    m_numberOfPoints = numberOfPoints;

....}
然后在循环函数中,我移动它们,但据我所知,这不会改变它们的值:

for (unsigned i=0;i<m_numberOfPoints;++i)
{
    shiftedXValue = (((m_xPointValues[i] - m_xAbsoluteMin)/(m_xAbsoluteMax-m_xAbsoluteMin))*(m_roi[2]-m_roi[0]))+m_roi[0];
    shiftedYValue = (((m_yPointValues[i] - m_yAbsoluteMin)/(m_yAbsoluteMax-m_yAbsoluteMin))*(m_roi[3]-m_roi[1]))+m_roi[1];
}

用于(无符号i=0;i这听起来像是
xPointValues
yPointValues
指向的位置,您传递给
setPointValues
,它们与
m\u xPointValues
m\u yPointValues
没有相同的生存期。谢谢!这是我声明它们的方式。我最初的名字是:float xPointValues[numberOfPoints];但通过将其更改为:float*xPointValues=new float[numberOfPoints];函数运行后,它们将不再被删除。