George Sha George Sha - 4 months ago 18
C Question

Can't implement struct with 2-d array in it in C

For example I have the following 2 structs in C:

typedef struct {
int index[128][128];
int value;
} x;

typedef struct {
x allx[128];
} y;

And in the main function I'm trying to use them:

int main(int argc, char *argv[]) {

x x1 = {{{1,2},{3,4}}, 1};
x x2 = {{{2,4},{1,5}}, 0};
x x3 = {{{3,6},{1,7}}, 1};

y y1 = {{x1,x2,x3}};

printf("%s\n", "Test");

However, after I compile and run it, nothing happens. The printf is to test if the program work can run. But it just does nothing, never prints the "Test". And during the compile, no error happens. I don't know if it's the problem of the 2-d array in the struct x. Anyone can help?


It's likely stack overflow. A single y on the stack is 128*128*4*128 which is 8 meg. On windows the default stack is 1 meg (or it used to be) and linux is like 8 meg I think.

When I shrink y to

typedef struct {
    x allx[8];
} y;

Or make the variables static, it runs fine on Xcode.