"Heap":
The heap is the name for a block of RAM that is used to store dynamic variables, that is variables created by malloc (in C), or new (in C++). (Modified From About.com)
"Stack":
In computer science, a stack is an area of memory that holds all local variables and parameters used by any function, and remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are "pushed onto" the stack. When the function returns, these locals and parameters are "popped." Because of this, the size of a program's stack fluctuates constantly as the program is running, but it has some maximum size. (From Wikipedia.org)
"Growing dow the address space":
A stack is usually represented in computers by a block of memory cells, with the "bottom" at a fixed location, and the stack pointer holding the address of the current "top" cell in the stack. The top and bottom terminology are used irrespective of whether the stack actually grows towards lower memory addresses or towards higher memory addresses.
Pushing an item on to the stack adjusts the stack pointer by the size of the item (either decrementing or incrementing, depending on the direction in which the stack grows in memory), pointing it to the next cell, and copies the new top item to the stack area. Depending again on the exact implementation, at the end of a push operation, the stack pointer may point to the next unused location in the stack, or it may point to the topmost item in the stack. If the stack points to the current topmost item, the stack pointer will be updated before a new item is pushed onto the stack; if it points to the next available location in the stack, it will be updated after the new item is pushed onto the stack. (From Wikipedia.org)
I guess ultimately it depends on the computer hardware upon which your program is running. The size of the stack is usually configurable, within limits, by the compiler.
A "stack" is an abstract data structure which stores data in a LIFO (Last In, First Out) manner. Typically, a stack has 3 main operations: "push( )" (adds data onto the top of the stack), "pop( )" (removes the last item on top of the stack), and "top( )" (gets the object on top of the stack). When an object is pushed onto the stack, it sits on top of the object that was pushed last. If a push operation exceeds the upper-bounds of the stack, the stack is considered to be overflown. When an object is popped from the stack, the last object that was pushed onto the stack is removed. If a pop operation exceeds the lower-bounds of the stack, the stack is considered to be in a state of underflow.
A "heap" is an area of memory which can be increased or decreased in size. This area of memory sits between the memory-mapped region for shared libraries, and the run-time heap. The area of memory is used for dynamic memory allocation. Beyond the memory-mapped region for shared libraries, resides the program's stack.
A "buffer" is typically an area of memory which is used to transfer data from one location to another. In some cases, a buffer is used to hold data from the disk while data-manipulating operations are performed.
Thank you all for helping me understand these terms.
BTW, how can I make sure I do not overflow or underflow any of these?
And also, how does the buffer for read(), write(), cin(if any) and cout(if any) work.
Please spend a portion of your precious time to help me understand. Thanks in advance.
Well, a program's stack almost never goes into the underflow state. However, the overflow state is quite common with recursive functions. As for prevention, it's quite hard to provide security measures on a high level. Personally, I don't know of any pre-cautions except for avoid recursive functions unless necessary.
Aceix wrote:
"And also, how does the buffer for read(), write(), cin(if any) and cout(if any) work."
Basically, n unformatted bytes are read from a set position. The read bytes are then placed into a buffer (a char array of some sort).
Could you be a bit more specific with the last question?
Ok are cin and cout stream objects?--of course yes. So the question is when I try to cout or cin data, is there any buffer?
Eg:
cinĀ»x;
Is x(char) a type of buffer?
And to end it up, apart from the basic buffer type, an array, can you give other examples of buffers in c++, and what makes the soo special to be called buffers?
Yes, there's a buffer for both of them. Stream objects have their own internal buffer. Take "std::cout" for example. When a string is passed to "std::cout", the string is added to the output buffer. At certain points in a program, or when explicitly requested, the output buffer is flushed to the screen.
During an input operation through "std::cin", the entered data is placed within the input buffer. The input buffer is then read based on the type of data the program wanted. The data is then extracted and placed within the location specified by the operand. For example:
1 2 3 4
int input(0);
// Read an "int" from the input buffer and place the data inside "input":
std::cin >> input;
A piece of memory qualifies as a buffer if its used to transfer data, or store data temporarily. However, the usage of a variable, array, or object can be used to determine whether or not the storage is used as a buffer. Examples of buffers are as follows:
1 2 3 4 5 6
unsignedchar *memory(newunsignedchar[(3 * sizeof(int))]);
// "memory" is a buffer for objects created with placement "new".
unsignedchar array[12] = {0};
std::cin.getline(array, 11);
// "array" is a buffer used to transfer 11 characters from the input buffer.