Getting a stack overflow exception when declaring a large array
Solution 1
Your array is way too big to fit into the stack, consider using the heap:
int *sieve = malloc(2000000 * sizeof(*sieve));
If you really want to change the stack size, take a look at this document.
Tip: - Don't forget to free your dynamically allocated memory when it's no-longer needed.
Solution 2
There are 3 ways:
- Allocate array on heap - use
malloc()
, as other posters suggested. Do not forget tofree()
it (although formain()
it is not that important - OS will clean up memory for you on program termination). - Declare the array on unit level - it will be allocated in data segment and visible for everybody (adding
static
to declaration will limit the visibility to unit). - Declare your array as
static
- in this case it will be allocated in data segment, but visible only inmain()
.
Solution 3
You would be better off allocating it on the heap, not the stack. something like
int main(int argc, char* argv[])
{
int * sieve;
sieve = malloc(20000);
return 0;
}
Solution 4
That's about 7MB of stack space. In visual studio you would use /STACK:###,### to reflect the size you want. If you truely want a huge stack (could be a good reason, using LISP or something :), even the heap is limited to small'sh allocations before forcing you to use VirtualAlloc), you may also want to set your PE to build with /LARGEADDRESSAAWARE (Visual Studio's linker again), but this configure's your PE header to allow your compiled binary to address the full 4GB of 32'bit address space (if running in a WOW64). If building truely massive binaries, you would also typically need to configure /bigobj as an additional linker paramerter.
And if you still need more space, you can radically violate convention by using something simular to (again MSVC's link) /merge:, which will allow you to pack one section into another, so you can use every single byte for a single shared code/data section. Naturally you would also need to configure the SECTIONS permissions in a def file or with #pgrama.
Solution 5
Use malloc. All check the return type is not null, if it is null then your system simply doesn't have enought memory to fit that many values.
Related videos on Youtube
Patrick McDonald
.NET Developer, F#, C#, ASP.NET MVC, previously VB.NET, VB6
Updated on January 10, 2020Comments
-
Patrick McDonald over 4 years
The following code is generating a stack overflow error for me
int main(int argc, char* argv[]) { int sieve[2000000]; return 0; }
How do I get around this? I am using Turbo C++ but would like to keep my code in C
EDIT:
Thanks for the advice. The code above was only for example, I actually declare the array in a function and not in sub main. Also, I needed the array to be initialized to zeros, so when I googled malloc, I discovered that calloc was perfect for my purposes.
Malloc/calloc also has the advantage over allocating on the stack of allowing me to declare the size using a variable.
-
Patrick McDonald about 15 yearsI'm pretty sure this type of question must have come up previously on this site, but searching for "stack overflow" is no use whatsoever
-
Vincent Floriot about 15 yearsi think every single C programmer ends up wasting a lot of their time figuring out this problem for the first time..
-
phuclv over 10 yearsTurbo C++ is a 16-bit application which means that it uses memory segmentation, each segment is 64KB in size so no structure can be larger than this number, and the total memory usage is maxed at 640KB (1MB or more with some extended memory manager). Why do you need to use such a more-than-20-year-old compiler?
-
Patrick McDonald over 10 yearsI just needed a free C compiler at the time for Windows and it seemed to compile the examples I tried from a 25-year-old book. What free compiler would you recommend now?
-
hat almost 6 years@MahlerFive, you have given me hope!
-
ryyker over 4 yearsBy now hopefully you have discovered GCC. Among other places It comes bundled with the Code::Blocks IDE.
-
Peter Cordes over 2 yearsRelated: C++ canonical Q&A answer that suggests new/delete or std::vector for the same problem: Segmentation fault on large array sizes
-
-
Christoph about 15 yearsI'd just make it static:
main()
should only be called once, so there are no pitfalls; no need formalloc()
here... -
Patrick McDonald about 15 yearsI am using problems from ProjectEuler.net to learn C, and am implementing the Sieve of Eratosthenes algorithm, so it does have to be that big. malloc works fine for my purposes though
-
aib about 15 yearsAs this is C, you need not (and in fact, should not) cast the return value of malloc.
-
Admin about 15 yearsWhy wouldn't you cast the result of malloc? Wouldn't you have to cast it from void* in order to do much of anything with it?
-
jweyrich about 13 years@yodaj007: You don't need to cast it explicitly. Since the assigned variable is also of a pointer type, the assignment performs an implicit conversion.
-
paulm almost 10 yearsAnd check sieve for NULL
-
DragonLord over 8 years"I am only sieve for you"--The Zohan
-
WhozCraig over 8 years@Amy Read this: "Do I cast the result of
malloc
?". It explains several reasons why not to castmalloc
(or any of the other memory allocation functions) in C. -
Toby Speight almost 6 yearsI think you meant
sieve = malloc(20000 * sizeof *sieve)
- unless your platform hasint
of size 1 (and even then, I wouldn't embed that assumption in the code). -
Peter Cordes over 2 yearsRelated: C++ canonical Q&A answer that suggests new/delete or std::vector for the same problem: Segmentation fault on large array sizes