Hi, I am writing a tool in C, but can't get rid of this error. I get a "EXC_BAD_ACCESS" on line afile = fopen(argv[3], "r");, which is right after where I try to init a few matrices.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, const char * argv[])
{
int w, h;
char line[64];
char * pch;
int frame, idx_x, idx_y, depth;
int mvx, mvy;
FILE *afile;
FILE *bfile;
if (argc != 5) {
printf("Usage: [...]");
exit(1);
}
w = atoi(argv[1]);
h = atoi(argv[2]);
int mtrx_depth[w][h];
int mtrx_mvx[w][h];
int mtrx_mvy[w][h];
afile = fopen(argv[3], "r");
bfile = fopen(argv[4], "r");
if (afile == NULL || bfile == NULL) {
printf("ERROR: Could not read file");
fclose(afile==NULL ? bfile : afile);
exit(1);
}
// Main loop 1: iterate over depth values in bfile, fill table
while (fgets(line, sizeof line, bfile) != NULL) {
pch = strtok(line, ",");
frame = atoi(pch);
pch = strtok(NULL, ",");
idx_x = atoi(pch);
pch = strtok(NULL, ",");
idx_y = atoi(pch);
pch = strtok(NULL, ",");
depth = atoi(pch);
mtrx_depth[idx_x][idx_y] = depth;
}
// Main loop 2: iterate over lines in afile, fill table
while (fgets(line, sizeof line, afile) != NULL) {
// Read line vars
{
pch = strtok(line, "\t");
frame = atoi(pch);
pch = strtok(NULL, "\t");
mvx = atoi(pch);
pch = strtok(NULL, "\t");
mvy = atoi(pch);
}
mtrx_mvx = mvx;
mtrx_mvy = mvy;
}
fclose(afile);
fclose(bfile);
return 0;
}
The error:
Running…
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
warning: Unable to restore previously selected frame.
No memory available to program now: unsafe to call malloc
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/Applications/Xcode.app/Contents/PlugIns/GDBMIDebugging.xcplugin/Contents/Resources/PBGDBIntrospectionSupport.A.dylib")
My guess is it's something to do with a (lack of) malloc for the three matrices, but I thought it worked earlier and don't really see how I should malloc them then...
Any help/suggestions would be welcome.
Thanks in advance!
Xavier