Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between memory, buffer and stack? [duplicate]

While learning C programming only the memory is mentioned, but in practice it seems that there is more; the buffer and stack words are also used.

What is the difference between these terms? Why are they relevant?

Please, I need a detailed answer. Short answers can't serve. Also some resources may be helpful.

like image 877
rullof Avatar asked Dec 20 '13 20:12

rullof


People also ask

What is the difference between buffer and stack?

The key difference is knowing the difference between the stack and a buffer. The stack is the space reserved for the executing program to execute in. When you call a function, it's parameter and return info are placed on the stack. A buffer is a generic chunck of memory that is used for a single purpose.

What is the difference between memory and buffer?

Buffering is the process of preloading data into a reserved area of memory called buffer memory. Buffer memory is a temporary storage area in the main memory (RAM) that stores data transferring between two or more devices or between an application and a device.

What is the difference between stack and buffer overflow?

Stack buffer overflow is a type of the more general programming malfunction known as buffer overflow (or buffer overrun). Overfilling a buffer on the stack is more likely to derail program execution than overfilling a buffer on the heap because the stack contains the return addresses for all active function calls.

What is the buffer in stack memory?

A stack buffer is a type of buffer or temporary location created within a computer's memory for storing and retrieving data from the stack. It enables the storage of data elements within the stack, which can later be accessed programmatically by the program's stack function or any other function calling that stack.


2 Answers

A buffer temporarily stores data while the data is the process of moving from one place to another, i.e. the input device to the output device. You can say that buffer is a part of the memory. You can say that a buffer is a pre allocated area of the memory where you can store your data while you are processing it.

From here:

The buffer, on the other hand, is found mainly in the RAM and acts as an area where the CPU can store data temporarily. This area is used mainly when the computer and the other devices have different processing speeds. Typically, the data is stored in a buffer as it is retrieved from an input device (such as a mouse) or just before it is sent to an output device (such as speakers). However, the buffer may also be used when moving data between processes within a computer.

So, the computer writes the data up into a buffer, from where the device can access the data, as its own speed. This allows the computer to be able to focus on other matters after it writes up the data in the buffer; as oppose to constantly focus on the data, until the device is done.

Buffers can be implemented in a fixed memory location in hardware or by using a virtual data buffer in software, which points to a data buffer are stored on a physical storage medium. Majority of the buffers are utilized in the software. These buffers typically use the faster RAM to store temporary data, as RAM has a much faster access time than hard disk drives. A buffer often adjusts timing by implementing a queue or FIFO algorithm in memory. Hence, it is often writing data into the queue at one rate and reading it at another rate.

Stack is a collection of items in which the data are inserted and remove from one end called the top of the stack.

In computer science, a stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop

like image 114
Rahul Tripathi Avatar answered Oct 16 '22 20:10

Rahul Tripathi


Memory is a storage space where instructions and data, regarding programs, are stored. Buffer and stack both are the small section of the memory.

Buffer stores data temporarily while execution of the program.

Operating System Concepts (8th ED):

A buffer is memory area that stores data being transferred between two devices or between a device and an application.

On the other hand, a stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. A stack is a limited access data structure - elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top.

like image 38
haccks Avatar answered Oct 16 '22 19:10

haccks