线程1:生成EXC_BAD_ACCESS(Code = 1,address = 0x0)问题

时间:2015-03-01 05:18:06

标签: c++

我正在使用Apple LLVM编译器在Xcode中进行C ++开发。我初始化char pickbuf变量并分配我想要的值1所需的内存到第二行代码。但我得到一个空指针错误:请帮助我。

2DCDP4.h

  class CDP : public Const2DCDP{

    struct PICK
        {
            short x;            // Transverse connector
            short y;            // Vertical direction consolidated
            unsigned char x0;   // Transverse reduction limit
            unsigned char y0;   // Longitudinal reduction limit
        } ;

    public:

        char*   pickbuf =new char[1];   // Overlapping buffer allocate some 
        PICK*   pickup =new PICK [1];       // Backtrace buffer

    }
void getProjection(void);
    };

2DCDP4.cpp

#include "2DCDP4.h"

void CDP::getProjection(void){
char    *before=pickbuf;
if( before[(j-1)*di + (i-1)] == 1) //**Thread 1 : EXC_BAD_ACCESS (Code = 1, address = 0x0)**
{
//code 
}
}

1 个答案:

答案 0 :(得分:2)

您尚未为pickbuf分配任何内存,但尝试访问第二行pickbuf处的内存。要解决此问题,请在第二行之前为pickbuf分配一些内存:

char pickbuf[n];

char* pickbuf = new char[n];

n的大小足以满足您的需求。