C language (record) -- memory-related _ 2: Address and Management of memory, _ 2

Source: Internet
Author: User
Tags float double

C language (record) -- memory-related _ 2: Address and Management of memory, _ 2

This article is based on the embedded C Language

Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Memory addressing Method
The internal storage is logically a grid. These grids can be used to hold things (the content inside is the number stored in the memory). Each grid has a number, which is the memory address, the memory address (a number) corresponds to the space in the grid (essentially a space) and is permanently bound. This is the memory addressing method.
When the program is running, the CPU in the computer actually only knows the memory address, and does not care where the space represented by this address is, how to distribute these entities. Because the hardware design ensures that the grid can be found according to the address, the two concepts of memory unit are: Address and space are two aspects of memory unit.

Relationship between memory and Data Types
The basic data types in C are char short int long float double.
Int INTEGER (integer type, which is reflected in the same data bit width as the CPU). For example, 32-bit CPU, 32-bit integer, and 32-bit int integer.
The relationship between data types and memory is as follows:
Data types are used to define variables, which need to be stored and computed in the memory. Therefore, the data type must match the memory to obtain the best performance. Otherwise, it may not work or be inefficient.
For example, int is recommended for defining variables in a 32-bit system, because it is highly efficient. The reason is that the 32-bit system is also 32-bit in combination with the memory. Such a hardware configuration is designed to define 32-bit int type variables with the highest efficiency. 8-bit char variables or 16-bit short variables can also be defined, but the access efficiency is not high.
Another example: In many 32-bit environments, we actually define bool type variables (actually only one bit is enough) to implement bool using int. That is to say, when we define a bool b1, the compiler actually allocates 32-Bit Memory to store this bool variable b1. The compiler actually wastes 31-bit memory, but the advantage is high efficiency.

Memory Management

Speaking of memory management, we need to talk about the data structure and heap.

The simplest Data Structure-array. When many data types in the program need to be managed, a single variable will appear bloated and messy. In this case, an array is required to effectively manage the data, that is, corresponding to the memory. However, it is obvious that the advantages and disadvantages of arrays are exclusive from their usage features. Advantage: The array is relatively simple. subscripts are used for access and can be randomly accessed. Defect: All element types in array 1 must be the same; 2 the array size must be defined and cannot be changed once determined. For the advantages and disadvantages of arrays, struct will be on the stage.

Struct.When we manage different attributes of one or several things and reflect different variables in data, the array will say "I can't do it ". Struct is generated in this case. For example, to manage student information, you can create the following struct for unified and tidy management.

Struct student

{

Unsigned int age;

Char name [20];

}

A stack is a data structure. The C language uses a stack to store local variables. Stack is invented to manage memory. Its memory management features small memory and automation.

Local variables in C language are implemented using stacks.
When we define a local variable (int num) in C, the compiler will allocate a space (4 bytes) in the stack) used for this local variable (the top pointer of the stack will move to give space at the time of allocation, which is used for local variable, the memory address of the 4-byte stack memory is associated with the local variable name a defined by us.) The corresponding stack operation is the inbound stack.
Note: the movement of the stack pointer and memory allocation are automatic here (the stack is complete by itself, so we don't need to write code to operate it ).
Then, when our function exits, the local variable will perish. The corresponding stack operation is the stack (Out stack ). The four bytes associated with a in the stack space are released when the stack top pointer moves. This action is automatic, and no code intervention is required. Stack's small memory features can be said to be its shortcomings, therefore, we cannot define too many or too large variables when defining local variables in C language (for example, int a [10000]; when defining local variables).


Heap)Is a memory management method. Memory Management is a very complex task for the operating system, because the memory capacity is very large first, second, the memory needs to be irregular in terms of time and size (dozens, hundreds, and thousands of processes running on the operating system will apply for or release the memory at any time, memory block size applied for or released at will ).
Heap memory management is characterized by freedom (apply for and release at any time; free size blocks ). Heap memory is managed by the operating system to the heap Manager (a piece of code in the operating system belongs to the memory management unit of the operating system), and then to the user (user process) provides APIs (malloc and free) to use heap memory.
When should we use heap memory? When the memory capacity is large and needs to be used and released repeatedly, many data structures (such as linked lists) must use heap memory.
Features of heap memory management (large memory, manual allocation, use, and release)
1: unlimited capacity (capacity required for general use can be met ).
2: manual application and release are required. The meaning of manual application is that the programmer needs to write code to apply for malloc and release free. If the programmer does not release the memory after applying for memory usage, the memory will be lost (in the heap manager record, this memory still belongs to your process, however, the process itself thinks that the memory is no longer used, and will apply for a new memory block when it is used again. This is called Memory leakage.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.