getting stack overflow while creating byte array in Visual Studio 2010 - c++

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.

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

Define the local array as having static storage duration
static byte myArray[1316779];
Otherwise use standard class std::vector

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.

Related

Very large array - C array vs C++ array. Visual Studio - exceeds max (268435456)

I am trying to create a very large array, to which, I then get the following error.
char largearray[1744830451];
warning LNK4084: total image size 1750372352 exceeds max (268435456); image may not run
I was told I could use a C-array and not C++ . I'm not sure I fully understood my friend's response. I am currently using Visual Studio 6.0 C++ . Do I need to get another compiler to do straight C or is it a method to how to declare the array that needs to change?
If I need to change compilers, does someone have suggestions?
The char array[size] syntax means the array will be created in the data section of your compiled program and not allocated at runtime.
Win32 PE code cannot exceed 256MB (according to your linker's error message), but the array you're declaring is 1.6GB in length.
If you want a 1.6GB array, use malloc (and don't forget to call free!)
...but why on earth are you running VC6?
If you predefine the size, then you are restricted to the stack size (stack has less size but faster), so it is better to define the size dynamically, which means your data is stored in heap (heap has bigger size but a little bit slower than stack).
Have a look at http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html which explains the difference of stack and heap.

C++ stack overflow on Windows 8 and MinGW

I wrote the following C++ code. It is compiled on Windows 8 with MinGW (from msys). If I run it Windows will stop it and give a stack overflow error (C00000FD). What's the problem?
#include <iostream>
using namespace std;
class Test{
public:
int txt[1000];
};
int main(){
Test a[1000];
return 0;
}
What should I do, say, if I want to store a picture the size of 1920*1080? It'll be 1920*1080*4 bytes.
I believe that the default stack size in Windows is 1MB. As you are allocating 1000^2 ints, each of which is 4 bytes large, you trying to put more on the stack than it can hold.
Each Test object contains 1000 integers, likely clocking in at about 4kb each.
In main you are creating an array of 1000 objects for a total of 4MB. Your stack can't hold 4 megs.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686774%28v=vs.85%29.aspx says a common default is 1MB.
Note that
std::vector<Test> a(1000);
Will probably work just fine. std::vector does not store its contents on the stack like a local array does.
The Test object is at least 4000 bytes in size (depending on your platform's int size). You are trying to create an array of 1000 Test objects which will be 4,000,000 bytes or nearly 4 MB. This almost certainly exceeds the default stack size for your program. You could possibly change this with some compiler options, but the question should really be what are you trying to do?
You should store large objects on the heap instead of the stack.
You can in fact change the default stack size with the following option in MinGW according to this answer:
gcc -Wl,--stack,N
But again, the better thing to do is to not store your large objects on the stack.

Size of dynamic array vs static array

What is the maximum size of static array, and dynamic array? I think that there is no limit for dynamic array but why static arrays have a limited size?
Unhandled exception at 0x011164A7 in StackOverflow.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00482000)
This looks more like a runtime error. More precisely - stack overflow.
In most places the size of array is limited only by available memory. However, the limit on stack allocated objects is usually much more severe. By default, it's 1Mb on Windows and 8Mb on Linux. It looks like your array and other data already on the stack is taking more space than the limit.
There are few ways to avoid this error:
Make this array static or declare it at top level of your module. This way it will be allocated in .bss segment instead of stack.
Use malloc/new to explicitly allocate this array on heap.
Use C++ collections such as std::vector instead of arrays.
Increase stack size limit. On Linux this can be done with ulimit -s unlimited
The maximum size of an array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes. The actual limit may be less, depending on operating system implementation details.
Note that this has nothing to do with the amount of physical memory you have available. Even on a machine with substantially less than 1 GB of RAM, you can allocate a 2 GB array... it's just going to be slow, as most of the array will be in virtual memory, swapped out to disk.

Stack/heap overflow when declaring a large array

I was trying to declare a 1024 by 1024 float array but a window just popped up saying project_name.exe has stopped working... with options whether to debug or close program. Previously, I succeeded declaring 1000 by 2 int array. I've kind of searched the internet for possible cause, and they said its memory related issue, "stack/heap overflow" in exact. They said that it is even worse for the case of float.
I only need up to 5 or 6 decimal places.
Any advice or suggestion? I didn't face this issue in python nor in matlab. I am using Microsoft Visual Studio 2010.
Are you declaring this as a local variable in a function or method? If so it's a classic stack overflow. For VS2010 see http://msdn.microsoft.com/en-us/library/8cxs58a6%28v=vs.100%29.aspx
The reserve value specifies the total stack allocation in virtual memory. For x86 and x64 machines, the default stack size is 1 MB. On the Itanium chipset, the default size is 4 MB.
So a 1024x1024 array of floats (assuming 4 bytes per float) clocks in at a whopping 4mb - you've sailed right through the default stack limit here.
Note that even if you do have an Itanium you're not going to be able to use all of that 4mb - parameters, for example, will also need to be stored on the stack, see http://www.csee.umbc.edu/~chang/cs313.s02/stack.shtml
Now, you could just increase the stack size, but some day you're going to need to use a larger array, so that's a war of attrition you're not going to win. This is a problem that's best solved by making it go away; in other words instead of:
float stuff[1024 * 1024];
You declare it as:
float *stuff = new float[1024 * 1024];
// do something interesting and useful with stuff
delete[] stuff;
Instead of being on the stack this will now be allocated on the heap. Note that this is not the same heap as that mentioned by Robert Harvey in his answer; you don't have the limitations of the /HEAP option here.
Are you declaring this on the stack perhaps? Objects that big have to be on the heap!

c++ memory allocation question

im trying to create an array:
int HR[32487834];
doesn't this only take up about 128 - 130 megabytes of memory?
im using MS c++ visual studios 2005 SP1 and it crashes and tells me stack overflow.
Use a vector - the array data will be located on the heap, while you'll still get the array cleaned up automatically when you leave the function or block:
std::vector<int> HR( 32487834);
While your computer may have gigabytes of memory, the stack does not (by default, I think it is ~1 MB on windows, but you can make it larger).
Try allocating it on the heap with new [].
The stack is not that big by default. You can set the stack size with the /F compiler switch.
Without this option the stack size
defaults to 1 MB. The number argument
can be in decimal or C-language
notation. The argument can range from
1 to the maximum stack size accepted
by the linker. The linker rounds up
the specified value to the nearest 4
bytes. The space between /F and number
is optional.
You can also use the /STACK linker option for executables
But likely you should be splitting up your problem into parts instead of doing everything at once. Do you really need all that memory all at once?
You can usually allocate more memory on the heap than on the stack as well.