指针访问冲突问题

Bil*_*nks 0 c++ pointers

我知道会出现大量这些问题,但我已经尝试/搜索过一切都无济于事.

更新开始

测试类

#include "stdafx.h"

#include "testerClasser.h"

Tester::Tester(){

}

void Tester::GetNum(int * num){

    int num2 = 6;

    *num = num2;// error thrown here

}
Run Code Online (Sandbox Code Playgroud)

调用GetNum函数的示例

int _tmain(int argc, _TCHAR* argv[])
{

    int* num = NULL;

    Tester* tester = new Tester();

    tester->GetNum(num);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误Tester.exe中0x77c115de处的未处理异常:0xC0000005:访问冲突写入位置0x00000000. 更新结束

我有一个方法

void CCCamera::getEyeXYZ(float *pEyeX, float *pEyeY, float *pEyeZ) {

*pEyeX = m_fEyeX;
*pEyeY = m_fEyeY;
*pEyeZ = m_fEyeZ;
Run Code Online (Sandbox Code Playgroud)

}

void CCCamera::getEyeXYZ(float *pEyeX, float *pEyeY, float *pEyeZ) {

float* pEyeX = new float(10);
float* pEyeY= new float(10);
float* pEyeZ= new float(10);
this->m_pCamera->getEyeXYZ (pEyeX,pEyeY,pEyeZ);
Run Code Online (Sandbox Code Playgroud)

}

void CCCamera::getEyeXYZ(float *pEyeX, float *pEyeY, float *pEyeZ) {

float* pEyeX;
float* pEyeY;
float* pEyeZ;
this->m_pCamera->getEyeXYZ (pEyeX,pEyeY,pEyeZ);
Run Code Online (Sandbox Code Playgroud)

}

我试着打电话给这个方法

    float pEyeX;
float pEyeY;
float pEyeZ;
this->m_pCamera->getEyeXYZ (&pEyeX,&pEyeY,&pEyeZ);
Run Code Online (Sandbox Code Playgroud)

#include "stdafx.h"

#include "testerClasser.h"

Tester::Tester(){

}

void Tester::GetNum(int * num){

    int num2 = 6;

    *num = num2;// error thrown here

}
Run Code Online (Sandbox Code Playgroud)

甚至

int _tmain(int argc, _TCHAR* argv[])
{

    int* num = NULL;

    Tester* tester = new Tester();

    tester->GetNum(num);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

他们显然是我想念的人可以帮助吗?

Mak*_*zin 5

在下面的代码中,您尝试取消引用函数NULL内的指针GetNum.

int _tmain(int argc, _TCHAR* argv[])
{

    int* num = NULL;

    Tester* tester = new Tester();

    tester->GetNum(num); //will result in NULL pointer dereference inside GetNum!

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您需要的最有可能是以下内容:

int _tmain(int argc, _TCHAR* argv[])
{
    int num = 0;

    Tester* tester = new Tester();

    tester->GetNum(&num); // pass a pointer to local variable

    return 0;
}
Run Code Online (Sandbox Code Playgroud)