Creating a static array on the heap?
I need to create a very large array. Let us say 50 megabytes.
Can I be safe to create it as a normal static array? Will the compiler put it on the stack (possibly causing a stack overflow), or will it be smart enough to put it on the heap?
If there is no way to do so, is there an easy way to do it with malloc or 开发者_StackOverflow中文版"new" when program starts, but automatically free it when program ends?
As I understand it, static variables don't live on the stack. If they did, where would they go when you pop the stack frame they live in? Static function variables need to keep their state between calls, so logically, the static data should be kept on the heap.
Also, when the program ends, everything is automatically deallocated.
The easy way to do this is by using std::vector
std::vector data;
data.reserve(<Number of Elements);
or potentially std::deque (depending on your usage).
Will the compiler put it on the stack (possibly causing a stack overflow), or will it be smart enough to put it on the heap?
A stack overflow is when the theoretical stack and theoretical heap collide and intermingle. If the stack was going to overfow then the heap is also going to fail.
Some systems have a maximum size of stackframe (this is compiler and platform specific) see your compiler documentation for details. As a result it is usually better to allocate huge structures dynamically (though not directly).
std::vector does this (probably). It has a small local object presence but the main payload (usually) is implemented as a dynamic heap allocation.
It's better, in my experience, to allocate such a large array on the heap (thus via new) - I have seen a program core dump on a unix system after allocating 2 MB on the stack... If you want automatic deletion, you could use a smart pointer (say, boost::scoped_array). But, since you mention "deleting it automatically when the program ends", you actually don't have to do anything - the operating system will reclaim all of your process's memory when it terminates.
Anyway, you really should use std::vector instead of a raw array.
50 megabytes is not too much by today standards.
You can allocate it at the start of your program using C++ new operator, and deallocate it with delete[] at end (or at the start/end of a defined program section).
If this array represents e.g some file to be loaded, it's better to allocate it of course when the file is loaded into memory. Optimally, you can only map only a section of the file into memory (e.g: 1MB, 2MB, or another logical "unit" you want to use) (see MapViewOfFile in Windows and mmap in UNIX systems). This way you can load very large files without exhausting your virtual memory.
If you allocate it statically, it'll be allocated statically. In a typical case, there will be a record of some sort in the executable that specifies that a particular variable should be a zero-initialized block of size N. The loader will normally honor that, just like it allocates space for the program's code and such (e.g., it'll allocate address space but quite often not actual memory to back it until/unless you actually read/write that memory).
精彩评论