0

I'm using apple LLVM compiler for C++ development in Xcode. I initialise the char pickbuf variable and allocate the required memory I want to assign value 1 to second code of line there. But I'm getting a null pointer error: please help me.

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 
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rehan K
  • 193
  • 1
  • 3
  • 17

1 Answers1

2

You haven't allocated any memory to pickbuf, but try to access the memory at pickbuf on the 2nd line. To fix this, allocate some memory to pickbuf before the 2nd line:

char pickbuf[n];

or

char* pickbuf = new char[n];

Where n is a size large enough for you needs.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • could you please find my edited code. after allocating memory im getting the error. thank you very much dude. – Rehan K Mar 01 '15 at 12:43
  • 1
    @RehanK You try to access the array at index `(j-1)*di + (i-1)` and it crashes, so `(j-1)*di + (i-1)` is giving you an invalid index (i.e. less than 0, or more than/equal to the array size). Use the LLVM debugger to find out what `(j-1)*di + (i-1)` evaluates to and why. – Emil Laine Mar 01 '15 at 13:41
  • could you please check this question dude http://stackoverflow.com/questions/28832664/thread-1-exc-bad-access-code-2-address-0x8-error-c?noredirect=1# – Rehan K Mar 03 '15 at 13:24