Nope, it's on a stack space which could be used from another variable at a second time.
And im suppose to know this to be able to program in c++? |
You are, if you want them to perform and act good.
But wiki's aren't really much friendly.
How do you know how "much" you can store associated with each computer? |
If you cannot allocate enough memory, an exception is thrown.
You use
try/catch
to deal with it.
storing it in the stack is essentially not using new/delete and letting the end of the fuction delete it from returning? |
Also, but stack cannot allocate memory in arbitrary size.
The memory it allocates must be known at compile-time for stack.
One of the advantages of using dynamic memory (heap) is having unlimited objects in a game, as an example.
You don't want to be limited to 5000 on a gaming pc, do you? heheh.
Let me try to help you (In the hope this time my network does not crash as I send the message like it did last time).
Tutorial for the Stack:
About terminology, you can virtually "split" stack and heap, because if your stack is accessed in a bad position,
you get a "Stack overflow" error (Which is also another C++-based website).
Info in case you get this error:
The stack overflow *WILL* force your program to terminate as there's no solution to this kind of problem
(You actually "ate" your programflow-informations, "puking" your data on it).
Here's a sample of the Stack usage.
Remember you have a fixed stack usage allowed.
You use too much stack space, your program crashes, and we're in the 64kb line by default.
In these examples I WILL assume the most common sizes (4byte for int and pointers)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// --- Read the someFunction comments later! ---
void someFunction()
{
int myInt = 0; // Your old stack usage was 12 - Now it's 16.
return; // Now it's going back to 8, the old stack pointer is "released".
// Return to the main() comments.
}
// --- Begin here ---
int main()
{
//4bytes of stack used because of how the call systems work
int myInt = 0;
// Now, add 4 bytes used to your stack.
someFunction(); // have a function call: The pointer to the old stack is passed,
// add 4 bytes to the used quota. Now switch to the someFunction comments.
return 0; // You're on 8 bytes now, myInt and old stack pointer freed, you're back to 0.
}
|
As you can see, there is NO WAY the stack is able to "lose" memory.
It always automatically "frees" his memory, because of how the assembly code comes out.
Your stack is on a constant line of bytes long 64kb.
When you enter a function, your stack becomes shorter, because the new function sees the stack as if it was in another place.
See it like a long road.
________________
^ ^ ^
a b c
|
The road is always the same length.
But A, B, and C see the road from a different place.
Actually, A put all his cars in the space between A and B.
And B put his cars between B and C.
They're all stretched, because one wanted to come after the other one.
If A didn't put a car in there, B could have used one car more.
Now for C++, translate Road to Stack, Length to Size, Place to Offset and Car to Memory.
Tutorial for the Heap:
Heap is not a single "big" block of memory.
Heap is a random (usually small, of the size of 1kb) block of memory into your RAM.
(Also the stack is into your ram, but stack is in a fixed width between his beginning and his ending.
Say the stack could be in a RAM space between 0x1000 and 0x2000 <Theorical numbers!>.
The heap can be as wide as your PC allows, ranging from 0 to (maximum integer for your platform).
But, usually you will NEVER access address 0, and you will always see their addresses in a higher position.
Example: 0x0fcb03f2
What it means is, you're (translate 0x0fcb03f2 from hex to decimal) bytes far from the beginning of your RAM.
)
If this theory wasn't enough:
Heap can start at any address in your ram.
You simply request some memory from your OS.
Let's go into some code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
void ChangeValue(int * myInt)
{
// Set the memory given from windows to 0 or an arbitrary int value
*myInt = 0;
}
int main()
{
// "new int" can be translated into:
// Hey windows, I want a block of memory of size "sizeof(int)" (4 bytes) from you.
// Where can I find some?
int * myInt = new int;
// What can happen now is:
// 1. Windows has the memory and gives you a valid pointer
// 2. Windows doesn't have the memory and throws an exception.
// We'll deal with exceptions at a second time and we'll see what happens with a valid pointer
// myInt is now a valid pointer. You are free to use that memory.
ChangeValue(myInt);
// now "*myInt" is 0.
// "myInt" is a big number between 0 and the maximum address of your RAM,
// and it is called a pointer.
// The problem is, once you've used that memory, as the memory you get with the heap isn't one
// block right after the other, Windows does not know when it can give the same "memory packet"
// to another program (Or even to you a second time).
// To signal to windows you finished using it, you delete it.
delete myInt;
// To avoid using it again (It may crash your program,
// the value may be changed from another application or so on...),
// clear the pointer's value.
myInt = 0;
// Remember, it's not the same of "*myInt = 0;"
// Also myInt is pointing to memory you should (and can) not access.
// Doing "*myInt = 0;" at this point will crash your program with "Access Violation" error.
return 0;
}
|
Some more code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
// Now you're thinking, why so much for the heap?
// It's hard to use and I must keep track of everything!
// Let's do everything on the stack!
// What's the problem?
// 1. Size. As I said, you have a fixed stack size
// 2. The heap's power. You cannot allocate arbitrary sizes from the stack.
int main()
{
int mySize = (rand()%1024)+1; // Random memory size between 1 and 1024
// Allocate the memory depending on the random number you just received
int * myIntMany = new int[mySize];
// Careful: Don't use a size of 0 or a size too high.
// Try to split the memory in different memory blocks if you're going over 64kb of memory in use
for(int i = 0; i < mySize; i++)
{
myIntMany[i] = rand(); // You can safely access from myIntMany[0] to myIntMany[mySize-1]
}
delete[] myIntMany; // Another note:
/* int* myInt = new int;
* is followed by
* delete myInt;
* int* myInt = new int[myNumber];
* is followed by
* delete[] myInt;
* I think you are able to see the difference.
*/
return 0;
}
|
EDIT:
Oh wow I forgot exception handling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// Fast exception example
#include <iostream>
int main()
{
char * myTestAllocation = 0;
try {
myTestAllocation = new char[0xffffffff];
// Very big size, it's the 32-bit systems' ram limit.
// Remember: An int is 4 bytes. A char is 1 byte.
delete[] myTestAllocation;
} catch(std::exception& e)
{
// Wherever your program will crash because of a standard exception,
// you're given a second try to save your ass by ending up RIGHT here.
// finished the catch "block", you're back in the normal program flow,
// unless the exception came from a (con/de)structor.
std::cout << e.what(); // Print the error message
int i = 0;
std::cin >> i; // Rudimentary on-the-fly way to stop the message.
}
return 0;
}
|
Now for the questions?
P.S. :
Actually, you can use more memory.
For a 4GB system like mine, (even if 32-bit) i've been able to get almost all of the 3GB-limit.
Heck this must be my longer post, 7985 bytes wasted for all of you b**ches!
Fear me my dear twicker for I will reach the 8192 character limit one day!