Allocation of five major memory areas of the program (heap area, stack area, global static area, literal constant area, program code area) + buffer description

Program memory allocation:
1. Heap area (head):
Generally, it is allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS at the end of the program.
Note that it is different from the heap in the data structure, and the allocation is similar to a linked list.
2. Stack area (stack):
It is a special linear table that can only be operated at one end of the linear table. Operations are allowed on the top of the stack and not allowed on the bottom of the stack. The characteristics of the stack are: first in last out The operation of putting elements from the top of the stack is called stacking (push the stack), and taking out elements is called popping the stack (popping the stack).
The characteristics of the stack: first in, last out, the stack memory in Java is a stack data structure, the method called first will not be popped (popped) until the method called later ends; the compiler will automatically allocate and release, and store the
function Parameter values, values ​​of local variables, etc. The operation mode is similar to the stack in the data structure.
3. Global area (static area-static) :
global variables and static variables are stored together, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are adjacent Another area. Released by the system after the program ends.
4. Literal constant area:
The more constant strings are placed here. Released by the system after the program ends.
5. Program code area:
store the binary code of the function body.
6. Buffer:
1. Concept
Buffer is also called cache, which is part of the memory space. That is to say, a certain storage space is reserved in the memory space, and these storage spaces are used to buffer input or output data, and this part of the reserved space is called a buffer.
The buffer is divided into an input buffer and an output buffer according to whether it corresponds to an input device or an output device.
2. Why introduce a buffer
For example, when we fetch information from the disk, we first put the read data in the buffer, and then the computer fetches the data directly from the buffer, and then reads from the disk after the data in the buffer has been fetched, so that the disk can be reduced. The number of reads and writes, and the operation of the computer on the buffer is much faster than the operation on the disk, so the application of the buffer can greatly improve the operating speed of the computer.
For another example, we use a printer to print a document. Since the printing speed of the printer is relatively slow, we first output the document to the corresponding buffer of the printer, and then the printer prints it step by step. At this time, our CPU can handle other things.

Now you basically understand, the buffer is a memory area, which is used between the input and output devices and the CPU to cache data. It enables the low-speed input and output devices and the high-speed CPU to work in harmony, avoids the low-speed input and output devices from occupying the CPU, frees the CPU, and enables it to work efficiently.
Types of
buffers There are three types of buffers: fully buffered, line buffered, and unbuffered.

  1. Fully buffered
    In this case, the actual I/O operation is performed after the standard I/O buffer is filled. A typical example of full buffering is reading and writing to disk files.
  2. Line buffering
    In this case, real I/O operations are performed when a newline character is encountered on input and output. At this time, the characters we input are stored in the buffer first, and the actual I/O operation is performed when the Enter key is pressed to change the line. Typical representatives are standard input (stdin) and standard output (stdout).
  3. No buffering
    means no buffering, and the standard error condition stderr is a typical representative, which allows the error information to be displayed directly and as soon as possible.
    ANSI C (C89) requires caches to have the following characteristics:
    standard input and standard output are fully buffered if and only if they do not involve an interactive device.
    Standard error is never fully buffered.

However, this doesn't tell us whether standard input and output are unbuffered or line-buffered, and standard output is unbuffered or line-buffered, if they refer to an interactive device.
Most systems use the following types of caching by default:
Standard error is uncached.
If they are streams involving end devices, they are line-buffered; otherwise they are fully-buffered.
We often use standard input and output streams, and ANSI C does not have mandatory regulations on the caching characteristics of stdin, stdout, and stderr, so that different systems may have different caching characteristics of stdin, stdout, and stderr. At present, the main cache characteristics are: stdin and stdout are line caches; and stderr is no cache.
The size of the buffer
If we do not set the buffer ourselves, the system will set a buffer for standard input and output by default. The size of this buffer is usually 4096 bytes, which is related to the paging mechanism in the computer, because the process The mechanism of paging and segmentation is used to allocate memory in the computer, and the size of each page is 4096 bytes, so the size of the buffer is usually set to 4096 bytes.
Buffer refresh (clearing)
The buffer will be refreshed in the following situations:
①When the buffer is full;
②When the line buffer encounters a carriage return;
③Close the file;
④Use a specific function to refresh the buffer.
When the c language program is running in the window, if there are scanf(), getc() and other types of functions in the program, when typing a series of character data in the window, these character data are only stored in the buffer and not written into scanf() , getc() and other functions corresponding to the 'object', when the buffer is refreshed, it will enter the 'object' corresponding to these functions.
Why can I see the input content from the window when typing this series of characters in the window?
According to the previous printer example, the data to be printed is stored in the buffer area, freeing the CPU (no need to read and write a data into the printer all the time, the printing time is much longer than the CPU reading and writing once, with the cache CPU can read at one time A batch of data to the cache, the printer prints the data from the cache, and the cpu can perform other tasks), and when we type a character in the window, a character will be displayed, which improves interactivity (just like the printer does not have a cache When the cpu reads a data in the memory, the printer prints a data), and does not refresh the buffer area, so it is not written into the object corresponding to scanf() and getc()

Guess you like

Origin blog.csdn.net/Demondai999/article/details/123875264