7

How can I increase the stack size in Code::Blocks?

I've read this and it says default stack size in VS is 1MB. Now as far as I am concerned, it has nothing to do with VS and stack size is OS dependent. In my win10 case it is 1MB.

This seems a little bit outdated as following this: project->build options->linker settings->other linker options doesn't exist no more.

There's no build under project bar.

Anyway, I need to increase my stack size so I can declare a huge two dimensional char array and benefit from cache. Like arr[1000][1000]. As it will be on contiguous memory unlike char* arr[100] which will point to 1000 different memory addresses containing the 1000 bytes.

I'm using Windows 10 mingw compiler.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • There's nothing magical about the memory on the stack. It won't be any faster than memory allocated from the heap. – Harry Johnston Feb 24 '17 at 02:52
  • @HarryJohnston yes but having the whole thing in a contiguous slot will gain from cache. Unlike when its allocated on heap using `malloc` then I'll have 1000 different segmants and more cache miss. – Tony Tannous Feb 24 '17 at 15:07
  • 1
    Oh, I see - you think you'd have to allocate 1000 different 1000-byte blocks. You don't. Just allocate a single block of a million bytes, and cast it as an 2-dimensional array. You can't do that in Java, say, but it works fine in C/C++. – Harry Johnston Feb 24 '17 at 20:54
  • The syntax is a little tricky though: `char (*arr)[1000] = (char(*)[1000])malloc(1000*1000);` – Harry Johnston Feb 24 '17 at 21:56
  • @HarryJohnston an explanation please :) ? is this a way to allocate 1000 blocks of 1000 bytes and have them in a contiguous memory block ? if so, why not use a 2'd array in first place ? thanks for the comment though! – Tony Tannous Feb 24 '17 at 22:28
  • Increasing the default stack size is an inefficient way of doing this, because *every* thread will have a big stack even though only one thread needs it. In many situations it will make more sense to use a static array, but I assumed you'd have already done that if it was appropriate. I'm not sure what you need explained, the code in my comment allocates a single million-byte block and tells the compiler to use it as a 2D array. – Harry Johnston Feb 24 '17 at 22:54
  • @HarryJohnston I understand that now :). Thanks for help. – Tony Tannous Feb 24 '17 at 22:55

1 Answers1

6

The default size comes from the .exe, not the OS.

From MSDN:

The default size for the reserved and initially committed stack memory is specified in the executable file header.

Specifically, the stack reserve and commit sizes are specified in the IMAGE_OPTIONAL_HEADER structure in a PE file. This can usually be set to a specific value with a linker parameter. With the MinGW toolchain you can try something like -Wl,--stack,52428800 as a gcc parameter. This option might exist in the IDE you are using, just look for build and/or linker settings.

This applies to the first thread, other threads can override the default if you specify a non-zero value when you call CreateThread.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • But in this article http://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html says it is **OS dependent**... ? – Yousha Aleayoub Aug 20 '17 at 16:09
  • 2
    There's nothing in this answer that contradicts that. On the Windows OS, this particular header is used to specify a stack size. Only Windows uses PE files for its binaries, so other operating systems will behave differently. – Sean Burton May 31 '18 at 13:46