0

Using Visual Studio 2012, if I type the following:

char bytes[ 2*1024*1024*1024 ]; 

I get the error: "matrix size must be greater than zero". The same occurs if I declare the size dynamically, i.e.:

char* bytes = new char[ 2*1024*1024*1024 ];

If I remove the first "2", everything is fine. It seems as there's a hard limit on the amount of memory I can request either from the stack or the heap, being this limit 1 GB. However, given that size_t is 4 bytes in the worst case (it could be 8 bytes almost for sure), there is not any problem in the index not being able to address all the space of the array. Is the problem that the limit imposed to the stack and the heap is 1MB by default? (http://msdn.microsoft.com/en-us/library/f90ybzkh(v=vs.110).aspx). If this is the case, then why can I allocate 1 GB?

3
  • 2
    You're overflowing the size of an int in the size expression. Commented Oct 12, 2014 at 9:53
  • VC++ doesn't report 'Matrix size must be...'. It reports, 'Negative subscript' or 'the size of array must be greater than zero' Commented Oct 13, 2014 at 6:26
  • @Ajay Well, you may be right. I'm using Visual Studio in Spanish and I just made a literal translation of the compiler message. The translation in Visual Studio may be inaccurate though. Sorry if that creates confusion. Commented Oct 13, 2014 at 17:09

1 Answer 1

4

You need to take care not to overflow a 32 bit int expression - 2*1024*1024*1024 is 2^31, which is 1 larger than INT_MAX. Try:

char bytes[ 2ULL*1024*1024*1024 ];

Note that the compile error has nothing to do with stack or heap size. Whether you can actually allocate this amount of memory is a separate problem.

Sign up to request clarification or add additional context in comments.

7 Comments

1. "this is" or "this has"? 2. Worth mentioning that it cannot be executed on a 32-bit system (requires 64-bit address space).
@barakmanos: is that a Windows-specific limitation ? Most sensible 32 bit environments allow allocations > 2 GB.
Well I meant >4GB... In this case, 2GB is indeed supported on 32-bit systems, my mistake (your ULL got me thinking that it was larger than 4GB, so I didn't bother to do the math).
But I think that it still has a lot to do with stack/heap size. Perhaps you meant that the standard doesn't say anything about where the array is allocated. So in theory this statement is correct. But in practice, the array will be allocated in the stack, in the data-section or in the heap. If there is not "enough room" there, then the program will not execute correctly (or will not execute at all).
You're right that it is a size_t, but there is no implicit cast within the expression, so it's as if you wrote e.g. size_t size = 2*1024*1024*1024;, i.e. the damage has already been done before the value of the expression is converted to size_t.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.