C - Segmentation error in professor test code

My professor sends test code to run our program. However, the test code itself has a compilation segmentation failure error. The error occurs on the first printf. However, if this line is commented out, it just appears on the next line. The code seems to work just fine for him, so I'm trying to figure out why this is not for me. I know that it uses C while I use C ++, but even when I try to compile test code with gcc instead of g ++, it still fails. Does anyone know why I may have problems? Thank you The code is below.

#include <stdio.h> main() { double A[400000][4], b[400000], c[4] ; double result[4]; int i, j; double s, t; printf("Preparing test: 4 variables, 400000 inequalities\n"); A[0][0] = 1.0; A[0][1] = 2.0; A[0][2] = 1.0; A[0][3] = 0.0; b[0] = 10000.0; A[1][0] = 0.0; A[1][1] = 1.0; A[1][2] = 2.0; A[1][3] = 1.0; b[0] = 10000.0; A[2][0] = 1.0; A[2][1] = 0.0; A[2][2] = 1.0; A[2][3] = 3.0; b[0] = 10000.0; A[3][0] = 4.0; A[3][1] = 0.0; A[3][2] = 1.0; A[3][3] = 1.0; b[0] = 10000.0; c[0]=1.0; c[1]=1.0; c[2]=1.0; c[3]=1.0; for( i=4; i< 100000; i++ ) { A[i][0] = (12123*i)%104729; A[i][1] = (47*i)%104729; A[i][2] = (2011*i)%104729; A[i][3] = (7919*i)%104729; b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1 + (i%137); } A[100000][0] = 0.0; A[100000][1] = 6.0; A[100000][2] = 1.0; A[100000][3] = 1.0; b[100000] = 19.0; for( i=100001; i< 200000; i++ ) { A[i][0] = (2323*i)%101111; A[i][1] = (74*i)%101111; A[i][2] = (2017*i)%101111; A[i][3] = (7915*i)%101111; b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 2 + (i%89); } A[200000][0] = 5.0; A[200000][1] = 2.0; A[200000][2] = 0.0; A[200000][3] = 1.0; b[200000] = 11.0; for( i=200001; i< 300000; i++ ) { A[i][0] = (23123*i)%100003; A[i][1] = (47*i)%100003; A[i][2] = (2011*i)%100003; A[i][3] = (7919*i)%100003; b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 2 + (i%57); } A[300000][0] = 1.0; A[300000][1] = 2.0; A[300000][2] = 1.0; A[300000][3] = 3.0; b[300000] = 20.0; A[300001][0] = 1.0; A[300001][1] = 0.0; A[300001][2] = 5.0; A[300001][3] = 4.0; b[300001] = 32.0; A[300002][0] = 7.0; A[300002][1] = 1.0; A[300002][2] = 1.0; A[300002][3] = 7.0; b[300002] = 40.0; for( i=300003; i< 400000; i++ ) { A[i][0] = (13*i)%103087; A[i][1] = (99*i)%103087; A[i][2] = (2012*i)%103087; A[i][3] = (666*i)%103087; b[i] = A[i][0] + 2*A[i][1] + 3*A[i][2] + 4* A[i][3] + 1; } printf("Running test: 400000 inequalities, 4 variables\n"); //j = rand_lp(40, &(A[0][0]), &(b[0]), &(c[0]), &(result[0])); printf("Test: extremal point (%f, %f, %f, %f) after %d recomputation steps\n", result[0], result[1], result[2], result[3], j); printf("Answer should be (1,2,3,4)\n End Test\n"); } 
+4
source share
2 answers

Try changing:

 double A[400000][4], b[400000], c[4] ; 

to

 static double A[400000][4], b[400000], c[4] ; 

Your array A declaration has an automatic storage duration, which probably means that your system is stored on the stack. Your overall stack for your process is likely to be below this, and you are faced with a stack overflow.

On Linux, you can run the ulimit command:

 $ ulimit -s 8192 $ 

to see the stack size in kB allocated for the process. For example, 8192 kB on my machine.

+18
source

You have overflowed the limits of the stack. Your prof declares 15MB of data in the main stack frame. This is too big.

Since the lifetime of the ojbect declared at the top of main is essentially the whole program, simply declare the objects as static . Thus, they will be in the (relatively unlimited) data segment and have almost the same lifetime.

Try changing this line:

 double A[400000][4], b[400000], c[4] ; 

:

 static double A[400000][4], b[400000], c[4] ; 
+12
source

Source: https://habr.com/ru/post/1411182/


All Articles