I had some problems with an array of structs, so I wrote the following just to make a test:
#include <stdio.h>
#define MAX_NAME_LEN 16
#define MAX_VALUE_LEN 32
#define MAX_ELEMS_IN_BLOCK 16
#define MAX_BLOCKS_IN_FILE 4096
typedef struct massElem_t{
char name[MAX_NAME_LEN];
char value[MAX_VALUE_LEN];
} massElem_t;
typedef struct massBlock_t{
massElem_t header;
massElem_t rest[MAX_ELEMS_IN_BLOCK-1];
} massBlock_t;
int main(){
massBlock_t massFile[MAX_BLOCKS_IN_FILE]; /* should be 3,145,728 bytes */
printf("==arrays and variables declared\n");
return 0;}
I'm compiling it with
gcc -g0 arraytest.c -o arraytest.exe -Wall -Wextra -Wshadow -Wvla -pedantic-errors -ansi
It compiles successfully, but upon execution, it takes some seconds, and quits without errors, and without even printing the printf line. The array doesn't get allocated, or its allocation causes some (not reported) error. If I halve the array size (eg. defining MAX_ELEMS_IN_BLOCK as 8 instead of 16), it runs fine.
I tried compiling on linux to use libasan:
gcc -ansi -g3 -static-libasan -fsanitize=address -Wall -Wextra -Wshadow -Wvla -pedantic-errors -o arraytest arraysize.c
Again, it compiles successfully, and also runs correctly. To obtain an error on linux I have to make the size of the array 4 times bigger, for example defining MAX_ELEMS_IN_BLOCK as 64 instead of 16. That makes the final array of size 12,582,912 bytes. Libasan detects a stack overflow.
My question is: why the difference between win10 and linux? Is there a way to allocate such an array?
Thanks