'Change stack size in VSCode

I would like to declare and initialize a large 3D array on stack. The c function declares the large 3D array as:

#define NMATS 36
#define ROWS 10000
#define COLS 9

void myfunc(void)
{
  double mat[NMATS][ROWS][COLS];
// Initialize later ...
}

In VS Code, the command cl.exe /Zi /EHsc /Fe: C:\Users\usr\project\c\build\main.exe c:\Users\usr\project\c\src\main.c successfully builds the code. However, during runtime I get the error:

Unable to open 'chkstk.asm': File not found.

This indicates that my Stack Reserve Size is too small. However, I am relatively new to VS Code and would like to know how to increase the stack reserve size and specify the option for cl.exe.



Solution 1:[1]

As mentioned in the comments, do a dynamic allocation so you don't have to rely on compiler switches and architecture limitations:

#define NMATS 36
#define ROWS 10000
#define COLS 9

typedef double MAT[NMATS][ROWS][COLS];


void myfunc(void)
{
    MAT* mat = (MAT*)malloc(sizeof(MAT));

    // initialize the matrix by referencing it as `*mat`
    (*mat)[0][0][0] = 0;

    free(mat);
}

But if you insist on needing a stack allocation:

Assuming sizeof(double)==8, 36*10000*9*sizeof(double) == 25,920,000. So you'd need at least that many bytes in stack space plus a little bit more for additional function call and local variable overhead. So let's add on another million bytes and round up to 27 million bytes.

If you really insist on having a stack allocation of 27 MB available. And I'm assuming you are using the Microsoft compiler:

Set the linker option: /STACK:27000000

The above works for all Visual Studio projects.

If your custom build step is just a single run of cl.exe (and not a separate linker step), you can have the compiler forward the command line option via the /F command parameter:

cl.exe /F27000000 /Zi /EHsc /Fe: C:\Users\usr\project\c\build\main.exe c:\Users\usr\project\c\src\main.c

https://docs.microsoft.com/en-us/cpp/build/reference/f-set-stack-size?view=msvc-170

https://docs.microsoft.com/en-us/cpp/build/reference/stack-stack-allocations?view=msvc-170

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