I've been trying to read an AIFF File into a number of variables and I have encountered a problem when trying to read the data part of the data chunk. I get the error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeee1bbce8). I know this usually means theres a problem with the pointer however I have checked and run so many tests and the pointer is not hitting the end of the file so its not that. Please could someone have a look I've tried to make a very simplified version to show the problem I'm getting.
int main()
{
FILE * AIFF;
int32_t Datasize = 27539504;
int16_t Data[(Datasize - 8)/2];
AIFF = fopen("/Volumes/Audio CD/Audio Track.aiff", "r");
fread(Data, 2, (Datasize - 8)/2, AIFF);
return 0;
}
The error comes up on the fopen line. Also this is in C++ in Xcode on a 64bit Mac.
Thanks.
Your code doesn't appear to check to see if fopen()
returns NULL... so if fopen()
fails for whatever reason (most likely because the specified file doesn't exist or can't be opened for reading), then your program will crash when fread()
tries to dereference the NULL pointer.
Side note: since .AIFF files are binary files, not ASCII, you should be passing "rb" as the second argument to fopen()
rather than "r".