c language advanced ---- memory 1

1 The purpose of the program is nothing more than two == get the result or get the process.

The running process of a computer program is actually a process in which many functions in the program run one after another.
The program is composed of many functions, the essence of the program is the function, and the essence of the function is the action of processing the data


Von Neumann Structures: Data and Code Together.
Harvard structure: Data and code exist separately.
What is Code: Functions
What data: global variables, local variables.


Dynamic memory DRAM:
Static memory SRAM:


Why memory is needed: Memory is used to store variable data, and data is represented as global variables and local variables in the program.
(In gcc, in fact, constants are also stored in memory)
(In most microcontrollers, constants are stored in flash, that is, code segments)
         Memory is critical for writing programs. Simpler programs require less memory.
Data Structures: The study of how data is organized
Algorithms: Research more efficient ways to process data.


How to manage memory: From the perspective of the operating system: it is managed in units of pages (4kB), and the pages are managed in a finer way (bytes).
     From a language perspective: different languages, different memory management interfaces (c/c++: malloc, free/new, delete c#/java virtual machine).


2 What is memory: Logically speaking: memory is actually composed of an infinite number of memory cells, each cell has a fixed memory address, which uniquely corresponds to this memory cell and is permanently bound.

Memory bit width: In actual hardware, there are: 8-bit 16-bit 32-bit 64-bit (other logical bit widths are not available on the market).


Bit and byte: bit (1bit) byte (8bit) half word (usually 16bit) word (usually 32bit).


Half word=1/2*word Double word=2*word.


On the linux+ARM platform, the word is 32 bits.


3 Memory addressing method: Memory is logically a grid that can hold things (data) one by one. There is a number, that is, the memory address (number) corresponds to this grid space one by one, and it is permanently bound. This is the memory addressing method. .
        When the program is running, the cpu only knows the memory address, and does not care where the space represented by the address is. The hardware design ensures that this space can be found according to this address.


Key: Memory addressing is in bytes, that is, the space size corresponding to a memory address is fixed, which is one byte (8bit).


Data type and memory: int (integer)--integer data, this whole is reflected in the fact that it has the same data bit width as the cpu itself, such as 32-bit cpu, integer is 32 bits = int is 32 bits.
The relationship between data types and memory is: data types define variables, variables are stored in memory, and data types and memory match to obtain the best performance

Memory alignment: hardware implementation, unaligned access is also possible but inefficient.


Memory addressing looks at the meaning of the array: the first element of an int a[] array is a[0], which has 32 bits and 4 bytes, and the first address is the address of the first byte of a[0].


4 How does c language operate memory
int a: The compiler applied for an int type memory grid for us (the length is 4 bytes, the address is definite, but only the compiler knows, we don't need to know) and bind a to this grid.

The essential meaning of data types in c language: it represents the length and parsing method of a memory grid.
Parsing method: both int and float are 4 bytes, but the storage form is different, and the parsing method is different.
Type coercion: changing length or parsing method


In c language: a function is the encapsulation of a piece of code, and the function name is the first address of the code



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528997&siteId=291194637