1

I have simple method:

void loadContent()
{
    int test[500000];
    test[0] = 1;
}

when I call this method, this error occurs:

Unhandled exception at 0x00007FF639E80458 in RasterizeEditor.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x000000AE11EB3000).

I changed the arraysize to 100 and everything is working fine... but I need 500000 integer values, so what should I do? C++ must be able to manage 500000 int values in one array, can the problem be somewhere else? or does I just have to change a setting of visual studio 2015?

Thomas
  • 2,093
  • 3
  • 21
  • 40
  • Yes, I know that this method is doing nothing, but I'll change it later ;) – Thomas Apr 11 '18 at 08:55
  • 2
    You should use `std::vector`. – molbdnilo Apr 11 '18 at 08:55
  • @molbdnilo but I don't need to change its size... so an array I think would be make more sense – Thomas Apr 11 '18 at 08:56
  • something is wrong with my visual studio i think, because 500.000 int values are nothing... I used arrays with bigger size in past – Thomas Apr 11 '18 at 08:58
  • 500k `int`s is (probably) around two megabytes. That's a lot for the stack. `std::vector` will allow you to transparently use the heap instead. – Quentin Apr 11 '18 at 09:01
  • The default stack size is usually 1 megabyte. Not enough to allow you to declare such a large array as a local variable. Setting the stack size of the primary thread is a linker option in VS, Project > Properties > Linker > System > Stack Reserve Size setting. – Hans Passant Apr 11 '18 at 09:02
  • okay thanks a lot! so I'll use std::vector or dynamic arrays instead. have a nice day ;) – Thomas Apr 11 '18 at 09:05

2 Answers2

1

When you create array like this, it's located into the "stack", but apparently your stack'size isn't sufficient.

You can try to place it into another place, like the "heap" (with dynamic memory allocation like malloc) like:

#include <stdlib.h>
#include <stdio.h>

void loadContent()
{
  int    *test = malloc(sizeof(int) * 500000);

  if (!test) {
     fprintf(stderr, "malloc error\n");
     exit(1);
  }
  test[0] = 1;
}
Paul-Marie
  • 874
  • 1
  • 6
  • 24
1

In visual studio you can adjust the stack size your application uses. It is in the project properties under linker system.

In general you don't want to do that as it makes things fragile. Add a few more functions and you need to go back and adjust it again.

It is better to do as suggested and use malloc or better std::vector to get the memory allocated on the heap.

Alternatively if it is safe to do so you can declare your array at the file level outside the function using static or an anonymous namespace to ensure it is private to the file. Safe here means your program only calls the function once at any time.