Home Dashboard Directory Help
Search

Large arrays with const size can crash at run-time on win64 by Katwell


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 663659
Opened: 4/20/2011 12:54:01 PM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

The following program, compiled on the command-line (x64 Debug Build in this case) with:

cl /EHs test.cpp

will crash at run-time instead of get caught in the exception handler.


int main()
{
    try
    {
        const // comment out, and the program works
        size_t size = 1ULL << 34;
        char* ptr = new char[size];
        ptr[size-1] = 0;
        delete[] ptr;
        return 0;
    }
    catch (...) {}
    return 1;
}

If the "const" is removed, the program will run without crashing.

This seems to be a long-standing issue, demonstrable in Visual C++ 2005, Visual C++ 2008, Visual C++ 2010, 2010 SP1, and SDK 7.1.
Details
Sign in to post a comment.
Posted by Microsoft on 8/18/2011 at 11:47 AM
Hi:
    A fix for this issue has been checked into the compiler sources.
    The fix should show up in the next release of Visual C++.

Xiang Fan
Visual C++ Compiler Team
Posted by Jonathan Jones MW on 5/4/2011 at 12:02 PM
Actually, on 64-bit Windows, the program above should compile and run cleanly. The allocation should succeed, and the entire 16 GB array should be accessible.
Posted by Microsoft on 5/4/2011 at 10:17 AM
Thanks again for reporting this. It looks like the compiler is internally truncating the array size to 32-bits. The result is that constant value 0x400000000 becomes 0, and thus the compiler is allocating an array of size 0. The result is an Access Violation when writing to the array.
When the value is not a constant the compiler correctly throws the bad_alloc exception which can be caught by the handler.
To work around this you can use the 32-bit cross compiler (32-bit compiler targeting 64-bit processors) which correctly generates a compile time error for your example:
error C2148: total size of array must not exceed 0x7fffffff bytes

thanks,
ian Bearman
VC++ Code Generation and Optimization
Posted by Microsoft on 4/20/2011 at 9:59 PM
Thank you for reporting the issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.These specialized experts will follow-up with your issue.
Posted by Microsoft on 4/20/2011 at 1:15 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.