我正在使用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
}
}
答案 0 :(得分:2)
您尚未为pickbuf
分配任何内存,但尝试访问第二行pickbuf
处的内存。要解决此问题,请在第二行之前为pickbuf
分配一些内存:
char pickbuf[n];
或
char* pickbuf = new char[n];
n
的大小足以满足您的需求。