Reputation: 10520
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?
Upvotes: 0
Views: 228
Reputation: 10520
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:
double (*X)[2] = malloc(2 * 1024 * sizeof(double));
Upvotes: 0
Reputation: 78353
double X[512][2];
for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
for (int i=0; i < 512; i++) {
int sample_offset = i * 2;
X[i][0] = samples[sample_offset];
X[i][1] = samples[sample_offset + 1];
NSLog(@"left: %f", X[i][0]);
NSLog(@"right: %f", X[i][1]);
NSLog(@"i: %d", i);
}
}
Upvotes: 2