[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Memory is the basis for running programs. All Running code is saved in the memory. The memory needs to process a variety of data, including keyboard data, mouse data, usb data, serial data, and camera data. After the data is processed by the program, output to the serial port, screen, usb, etc.
There is only one memory, but there are many types of space in the program. However, there are only several data types in the memory, such as global data, heap data, and temporary stack data. So what are their differences? We can find some problems through code.
(1) Global Data
Static int value = 100;
Void process ()
{
Static int number = 10;
}
Static int value = 100;
Void process ()
{
Static int number = 10;
} You can see here that the data of value and number actually belongs to global data, and the variables here do not change with the call of the function.
(2) Heap data
Void process ()
{
Char * point = (char *) malloc (100 );
Free (point );
}
Void process ()
{
Char * point = (char *) malloc (100 );
Free (point );
} The data allocated by the point here is the heap data. If there is no free operation, its existence is global. As long as the memory is not released, the memory will always exist.
(3) temporary data
Void process ()
{
Char name [100] = {0 };
Return;
}
Void process ()
{
Char name [100] = {0 };
Return;
} The data here is the data inside the stack. Once the process call ends and returns the data, the memory space indicated by the name address has been used by other functions. At this time, the memory space has no significance for us. Therefore, no matter how much space is used in the function, if you want to continue using the data before the function returns, you must copy the data before the function returns.
The content of this blog is relatively simple, mainly about some memory content. In fact, there are still many things about memory. Here, we will just give you an introduction:
1) Global Data is a type that we like to use and is easy to use.
2) Heap data is the space allocated by the system.
3) The stack space can only exist in the current function, and function return is meaningless.
Although we mentioned above, these three concepts can also be migrated to each other sometimes, for example:
1) Sometimes, we first build a global memory pool for testing purposes. In the future, the tested memory is allocated in the memory pool through custom malloc, the heap allocation is associated with the global allocation.
Global memory space <========> memory pool <========> local space allocation
2) If the function space we use is relatively small, all operations can be completed within a function. Is the global space consistent with the temporary stack at this time?
Global space <==============> local Stack
The above statement is a bit difficult, but our purpose is to make everyone understand:
A) You must always understand the space in which our data is stored.
B) Will the memory be out of bounds?
C) Will the memory leak?
D) whether the data accessed by the memory is still valid
[Notice: The following blog introduces the content of linear space]