I haven't used C in long time, and I'm having an issue filling a 2D array from a CSV. The file format is like this:
Node, In, Out
1,200,10393
...
This is essentially an array representation of a linked list. There are 150000 elements and whenever I try to fill the array I get an error saying "Unhandled exception at 0x000000013facb957 in main.exe: 0xC00000FD: Stack overflow." I'm on a 64-bit machine with 16GB of RAM and I'm using VS C++ 2010 Express with an x64 build configuration.
int main(int argc, char *argv[])
{
int counter = 0;
char line [ 1024 ];
int map[150000][2] = {0};
char *comma = ",";
char *token;
int index;
int in, out;
char* end;
int nodeID;
FILE *fp;
fp = fopen("mapsorted.txt","r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
//Skip header line
fgets ( line, sizeof line, fp );
while ( fgets ( line, sizeof line, fp ) != NULL) /* read a line */
{
//first part - the index for storage
token = strtok(line,comma);
index = strtol(token,&end,10);
//second part
token = strtok(NULL,comma);
in = atoi(token);
//third part
token = strtok(NULL,comma);
out = atoi(token);
//store in array
map[index][0] = in;
map[index][1] = out;
}
fclose ( fp );
}
The code seems to work when I allocate a smaller array, but fails when it is this large. I think I should have enough memory to be able to handle an array of this size.