force gcc to compile when stack-overflow is detected
How can i force gcc to compile 开发者_如何学JAVAa program in which i declare an array of unsigned integers that is bigger than my ram - without getting the warning warning: "integer overflow in expression"?
eg. i have 8gb of ram and i want do declare an array of 8.5gb.
edit:
thanks everybody, i figured out that the problem wasnt with the datatype but with some preprocessor stuff. it took me quite a while though. :) i wrote
#define GIBI 1073741824
#define ARRAYSIZE 2*GIBI
and obviously the compiler didnt like that.
integer overflow in expression
isn't a stack overflow, it means that GCC has detected that the result of your expression is causing an integral overflow in the lvalue you're assigning it to. For instance, if you try to malloc
something like INT_MAX * 2
, that expression will cause an integral overflow. If you cast it to a size_t
, you should be warning-free:
void *myvar = malloc((size_t) INT_MAX * 2);
size_t
is, of course, platform-dependent, and you could also overflow that if you can't represent the size of the block you want to allocate in sizeof(size_t)
bits. Basically, make sure you're using a type capable of representing the amount of memory you want to allocate and that it's compatible with size_t
(since that's what malloc()
expects, according to stdlib.h
).
That error message is not saying that you're going to run out of stack, though you will. It's saying that the number is too big to represent in size_t
. Make sure you're compiling for a 64-bit target.
Furthermore, don't do that. If you need to allocate gigantic amounts of RAM, do it with malloc
, not the stack. Or, better yet, with mmap
.
I'm fairly sure that "integer overflow in expression"?
does not mean that you are overflowing the stack but rather you are doing some math that may potentially overflow a signed integer which is Undefined Behavior. It would help if you paste the line the warning is referring to.
If you need to allocate more memory than you have RAM you might do some research on memory mapped files. That way you won't need to worry about the physical memory in your machine (of which you don't have access to all 8GB anyway)
http://msdn.microsoft.com/en-us/library/dd997372.aspx
精彩评论