jettisamba
jettisamba

Reputation: 711

getting stack overflow while creating byte array in Visual Studio 2010

I created a VC++ console application (HelloWorld). In that I created one byte array with size 1316779. It's working fine when building the application. Anyway debugging the application it's giving

Unhandled exception at 0x00969167 in HelloWorld.exe: 0xC00000FD: Stack overflow.

But when I changed size to 1010000 then it worked fine.

My requirement is to create a byte array size of 1316779. i.e. byte myArray[1316779];

I am working under XP operating system with Pentium processor and having 2GB RAM.

Upvotes: 0

Views: 117

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

Define the local array as having static storage duration

static byte myArray[1316779];

Otherwise use standard class std::vector

Upvotes: 1

Paul Evans
Paul Evans

Reputation: 27567

Obviously, 1316779 bytes are bigger than the stack and you're blowwing it up. So make the 1316779 bytes on the heap and pass a handle to that on the stack:

std::vector<byte> myHugeAmountofBytes;
myHugeAmountofBytes.reserve(1316779);
// put elemnts into myHugeAmountofBytes

myHugeAmountofBytes can now be passed via the stack with no overflow.

Upvotes: 0

Marco A.
Marco A.

Reputation: 43662

1316779 bytes = 1.31 MB

MSVC puts by default a 1 MB stack limit

/F - Without this option the stack size defaults to 1 MB.

You either need to increase that limit or reduce your stack size (e.g. use a heap allocation). I wouldn't rely on 1010000 as well (1.01).

Upvotes: 4

Related Questions