I have a for loop in my Objective-C code that keeps throwing a EXC_BAD_ACCESS error. Here is the code:
double (*X)[2];
for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
for (int i=0; i < 1024; i+=2){//numSamplesInBuffer / 2; i+=2) {
X[i][0] = samples[i];
X[i][1] = samples[i + 1];
NSLog(@"left: %f", X[i][0]);
NSLog(@"right: %f", X[i][1]);
NSLog(@"i: %d", i);
}
}
When i = 385, I get a EXC_BAD_ACCESS at the line NSLog(@"left: %f:", X[i][0]);.
Thinking it may be a memory issue with X being declared locally, I changed X to a property which caused a EXC_BAD_ACCESS at the first line of the for loop on the first time through.
Anyone know why this may be happening?
As HotLicks pointed out, the problem was that I wasn’t allocating any space for the array. The solution was to initialize the array like this: