I am afraid that the C language memory topic will be useless, so this article is just a discussion.
There are some differences between different platforms regarding memory issues. The platform mentioned in this article is x86 Linux platform
Using C language as a program (in fact, the same for other languages) requires not only familiarity with syntax, but also a lot of background knowledge. Before learning about memory allocation in C language, you must first understand what kind of address space Linux assigns to processes (running programs.
In general, there are three segments, namely the code segment, the Data Segment and the stack segment (friends who have learned the Assembly must be familiar with it ). Code segments are stored in program text, so sometimes called text segments. commands in the instruction pointer are obtained from here. This segment can be shared. For example, if you have two VI in Linux to edit the text, the two VI share a code segment, but the data segment is different (This point is similar to sharing the same member function for different objects in C ++ classes ). Data segments are used to store data. They can also be divided into three areas: the data zone initialized as non-zero, the BSS, and heap. The initialized non-zero data area generally stores static non-zero data and global non-zero data. BSS is block started
It is originally a term in assembly language. This area stores uninitialized global data and static data. There is also a heap. This region is used for dynamic memory allocation, that is, the memory allocated by using functions such as malloc is in this region. Its address increases upwards. The last stack segment (note that the stack is a stack, the heap is a heap, Not The Same Thing). The stack is too important. data such as local variables and function parameters are stored here. For example, recursive algorithms are implemented by stacks. Stack addresses increase downward. The details are as follows:
========= High address ======
Program stack segment
Downward Growth
"Empty" ======
Upward growth
Heap
------ Data Segment
BSS
------
Non-zero data
========= Low address ======
======================
Code snippet
======================
Note that the code segments and data segments are clearly separated, but the data segments and stack segments do not exist. In addition, the stack increases downward and the stack increases upwards, therefore, in theory, the heap and stack will "grow together", but the operating system will prevent such errors, so don't worry too much.
With the above theory, we will discuss the allocation of dynamic memory. As mentioned above, the dynamic memory space is allocated in the heap. The following functions implement dynamic allocation:
Stdlib. h:
Void * malloc (size_t size );
Void * calloc (size_t nmemb, size_t size );
Void * realloc (void * PTR, size_t size );
Void free (void * PTR );
Let's talk about it one by one. Malloc is to allocate a memory space of size, and use a void pointer to point to this space, and then return this pointer. That is to say, malloc returns a void pointer to the size space. If you want to use this space, you have to convert the void * type into a type you need, for example, int. Calloc and malloc are basically the same. The difference is that there are two points. One is that the size of the space allocated by calloc is determined by nmemb * size, that is, nmemb is the number of entries, the size can be viewed as the size of the entry, and calloc is used to calculate the total space. Second, the space returned by calloc is filled with 0, while malloc is not sure what is going on in the memory
West. Realloc is used to change the size of allocated space. The pointer PTR is of the void type. It should point to a space that needs to be reallocated, And the size parameter is the size of the whole space after the reallocation, rather than the increase. Similarly, a pointer to the new space is returned. Free is used to release the space allocated by the above three functions. Its parameter is a pointer to a space.
This is basically the case. These are basic topics. There are still many advanced topics and details. I will not explain them here. I will continue to summarize them if I have the opportunity.