'How to deal with the small stack size in MinGW?

I have the following code

#include <stdio.h>
//#define arraySize 5
#define arraySize 500
void func(double B[arraySize][arraySize])
{
    B[0][0] = 5;
}

int main(void) {
    double Ar2D[arraySize][arraySize];
    func(Ar2D);
    printf("%f", Ar2D[0][0]);
}

Code works fine when I test it in a linux virtual machine, but when I run it in minGW, it crashes if I set arraySize to 500 (works fine in minGW if I set arraySize to 5). What is the best way to fix this?



Solution 1:[1]

You really should avoid using the stack for large things.

It's also a security risk as stack overflows wan be abused by hackers (e.g. to try to attempt running arbitrary code).

Better is to use the heap, you can do this by replacing

double Ar2D[arraySize][arraySize];

with

double** Ar2D = (double**)malloc(arraySize * arraySize * sizeof(double));

and of course don't forget to free(Ar2D);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brecht Sanders