The pointer returned by new is not a memory block. It's a pointer to an array. An array is only truly considered a memory block when pointed to by a void *.
AR Khan's "solution" doesn't make any sense to me. What is it supposed to do? Stress-test the stack?
I've read it three times and I'm still not sure what OP is asking. I
think he's trying to allocate a ludicrously-sized array on the stack and the compiler is understandably complaining.
Let's go over memory allocation, shall we?
The stack
The stack is a memory region of fixed sized that a program uses to store local data and the local data of the function that has called the current function, and so on until we get to the entry point of the program. It should be assumed to be fairly limited, and nothing too huge should be stored on it.
Examples of data stored on the stack:
1 2 3 4 5
|
int a;
int b[10];
std::vector<int> c;
char d[]="string";
int e[ONE_MILLION]; //Likely to produce a stack overflow (see below).
| |
When too much data has been pushed onto the stack, usually because a poorly written recursive function ended up in infinite recursion or because a very large array was pushed, an error known as a stack overflow occurs. It's a very serious kind of memory corruption and potentially dangerous under the wrong conditions. The best thing that can happen after a stack overflow is that the system detects the program is trying to write to memory it shoudn't be and crashes it. All modern OSs (AFAIK) do this. The worst thing that can happen is the system allows the program to arbitrarily overwrite its own data or code, or another program's (including the OS's) data or code.
The heap
The heap is the rest of available memory, in few words. It's many times larger than the stack and is used to do most of the allocations in a program.
Examples of heap allocations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int *a=new int;
int *b=new int[10];
std::vector<int> *c=new std::vector<int>;
//Example d cannot be rewritten as a heap allocation in a single line.
int *e=new int[ONE_MILLION]; //Unlikely (but possible) to fail to allocate.
//All heap-allocated data has to be deallocated sooner or later:
//Notice that delete and delete[] are used in different cases. Using one when
//the other one should be used is dangerous.
delete a;
delete[] b;
delete c;
delete[] e;
| |
The only restrictions for heap allocations ar the size of the heap, and whether it's possible to allocate n
adjacent bytes.
For example: we have a heap of size 10, where all odd bytes are used. This leaves 5 even bytes free. In such scenario, trying to allocate 5 bytes will fail, even though there are, indeed, 5 bytes free. However, allocating 1 byte five times won't fail.
One final note that is sometimes relevant: stack allocation is always faster than heap allocation because the latter need to do system calls. The stack is preallocated and all it takes is to move a pointer back and forth.
Use the stack to store small things and local data. Use the heap to store big things, things of size unknown at compile time, and things that need to remain after the function returns.
Oh, an exabyte is EB or EiB, not XB.