Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation fault in C, can't seem to find the cause
    primarykey
    data
    text
    <p>I can't seem to find the cause of the segmentation fault in the code below. The code crashes in the line with the comment, after jumping back to main() from evaluate(). I looked at this code with gdb, and it seems everything is ok within the evaluate function - Output is filled in correctly, however after I return to main, nothing in the memory can be accessed. "p j" in gdb return an memory not accessible error, as does trying to print any other variable. I checked if the stack maybe had to many values, but increasing the stack size does not help.</p> <p>The crazy thing is that I can solve this error, but I have no idea why it changes anything. If I add, anywhere in evaluate(), an int declaration (eg. <code>int iAmNotUsedEver;</code>) then the code suddenly doesn't cause a segmentation error and works perfectly in gdb. Edit: Dynamically allocating node[116] in evaluate (<code>int *node = malloc(116*sizeof(int));</code>) also resolves the issue, but again I have no idea why?</p> <p>Evaluate function: (I removed part of the code, otherwise it would be way too long)</p> <pre><code>void evaluate(int *parameter, int output[][16]) { int node[116]; node[0] = 0; output[0][0] = !node[0]; node[1] = parameter[0]; output[0][1] = !node[1]; output[0][2] = !node[0]; ... node[34] = !node[114] &amp;&amp; !node[45]; node[45] = !node[34] &amp;&amp; !node[105]; output[11][15] = node[45]; } </code></pre> <p>Main function:</p> <pre><code>int main(int argc, char *argv[]) { int i; int j; int k; int ret; int parameter[8]; int output[12][16]; FILE *paramFile; FILE *outFile; paramFile = fopen(argv[1], "r"); if (paramFile == NULL) { printf("I couldn't open the parameter file.\n"); exit(0); } outFile = fopen(argv[2], "ab"); if (outFile == NULL) { printf("Something went wrong with the output file.\n"); exit(0); } while(1){ for(i=0;i&lt;8;i++){ ret=fscanf(paramFile, "%d", &amp;parameter[i]); printf("parameter: %d\n", parameter[i]); } if(ret!=1){ break; } for(j=0;j&lt;12;j++){ for(k=0;k&lt;16;k++){ output[j][k] = 2; } } evaluate(parameter,output); printf("Evaluation is done. \t %d\n",i); for(j=0;j&lt;12;j++){ //SEG FAULT HERE for(k=0;k&lt;16;k++){ fprintf(outFile, "%d", output[j][k]); } fprintf(outFile,"\n"); } fprintf(outFile,"\n\n\n"); } printf("Closing files\n"); fclose(paramFile); fclose(outFile); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload