Max_Int reached before the limit defined in climits.h
Frage
I get this error
whenever I enter whatever number bigger than the following, in a code compiled with Microsoft Visual C++ 2010 Express:
int size = 276447232;
Though, according to this conversation, this one or that one, I should be able to go up to 2147483646 before encountering any problem, no?
Sky
Lösung
The program is trying to allocate too much stack space:
char *outputGwb = char[size]; // array is created "on the stack"
Use malloc
(or new
in C++) to allocate memory from the heap. Make sure to free
(or delete
in C++) the memory later; just don't mix the allocation/deallocation strategies.
char* outputGwb = new char[size]; // C++: note use of the "new" keyword
char* outputGwb = malloc(size); // C: note no cast needed in a C compiler
This issue is thus about the maximum size of a particular resource and is not related to the maximum number an integer value can represent.
See What and where are the stack and heap? for an explanation between the two memory allocation areas. In addition, while I wouldn't necessarily recommend it, here is a thread that discusses how to change the stack size in Visual C++.