1

I get this error

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

Community
  • 1
  • 1
Robin Len'hog
  • 47
  • 1
  • 5
  • I don't think that assignment will cause the `abort()` .. how is the variable/value being used? That is, is there a *different* line that actually triggers the "Debug Error!"? –  Aug 15 '12 at 23:09
  • Hi pst, sorry for the delay. Indeed, I use this `size` in the next line: `char *outputGwb=new char[size];`. But the thing is that this error only appears when I change the `size` parameter... – Robin Len'hog Aug 21 '12 at 20:30

1 Answers1

1

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++.

Community
  • 1
  • 1
  • Indeed, I forgot to mention that I `delete` the memory later in the code. Though, I don't exactly get the change that it'd make: what is difference between _on the stack_ and _from the heap_? – Robin Len'hog Aug 23 '12 at 15:10
  • @RobinLen'hog It's very bad to delete (or free) something that was not new'd (or malloc'ed) :) I have updated the post with a link to a question talking about stack vs heap and added examples of heap allocation in C and C++. –  Aug 23 '12 at 19:22
  • Thank you very much. To be clearer than I might have before in this post, I have the 3 lines present in my program : `int size = 276447232;`, `char *outputGwb=new char[size];`, and `delete [] outputGwb;` with the rest of my code in between the `new` and `delete` lines. I'm gonna take a look at the links and examples, thx again – Robin Len'hog Aug 24 '12 at 20:09
  • After having browsed the links you gave me, I understand that I have asked for an allocation _from the heap_ (I already use `new`, that is why I `delete` at the end of my code). But when I go back to your answer I now read that you think that I "allocate too much _stack_ space". Is the _stack space_ directly related to allocating _from the heap_ or am I missing something? Thanks again – Robin Len'hog Aug 24 '12 at 20:23
  • @RobinLen'hog I missed that .. I thought I saw it was without the `new`. In any case, it appears to be an allocation that fails, although ~280MB isn't "that big". –  Aug 24 '12 at 20:43