Jump to content

Trying to increase the stack size in C.

Gat Pelsinger

This code compiles but doesn't print anything - 

#include <stdio.h>
#include <windows.h>

DWORD WINAPI ThreadFunction(LPVOID lpParameter) {
    // This function will run in a separate thread
    unsigned long long x[134217728];
    printf("%llu", sizeof(x));
    return 0;
}

int main() {
    // Set the thread stack guarantee for the main thread
    unsigned long stackSizeInBytes = 1568 * 1024 * 1024;
    SetThreadStackGuarantee(&stackSizeInBytes);

    // Create a thread with a specific stack size
    HANDLE hThread = CreateThread(NULL, stackSizeInBytes, ThreadFunction, NULL, 0, NULL);

    if (hThread == NULL) {
        // Handle thread creation failure
        fprintf(stderr, "Error creating thread: %lu\n", GetLastError());
        return 1;
    }

    // Wait for the thread to finish
    WaitForSingleObject(hThread, INFINITE);

    // Close the thread handle
    CloseHandle(hThread);

    // Wait for user input
    scanf(" ");

    return 0;
}

Do I instead using _alloca or the newer _malloca to achieve this? But why doesn't this work?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

The program doesn't print anything because it throws a stack overflow exception before it can reach the call to printf.

 

Adding this code right after the call to SetThreadStackGuarantee shows the error code 87.

DWORD err = GetLastError();
printf("%d\n", err);

 

Error code 87 corresponds to "ERROR_INVALID_PARAMETER" according to https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

 

In addition, the documentation for SetThreadStackGuarantee states that the value you pass into it cannot be larger than the reserved stack size. https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadstackguarantee

 

This means that using SetThreadStackGuarantee in this way isn't the right way to increase the stack size. This thread seems useful for doing that: https://social.msdn.microsoft.com/Forums/en-US/2a5b32b6-683b-4729-92d3-45ed7a89ef3f/stack-overflow-and-how-to-increase-the-stack-size?forum=Vsexpressvc

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

@dcgreen2k I don't use VS. I am on VS code. Is there anything else I can try? Are there any compiler or linker arguments I can try?

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, Gat Pelsinger said:

I don't use VS. I am on VS code. Is there anything else I can try? Are there any compiler or linker arguments I can try?

Maybe this will work? https://stackoverflow.com/questions/63134299/how-to-increase-max-stack-size-in-c-using-vscode

 

Also, is there a reason you're trying to allocate that much memory on the stack? Typically, the stack is 1MB on Windows and 8MB on Linux. The heap is specifically made to store large amount of data like what you're doing here.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×