In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use C language dynamic memory allocation, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use dynamic memory allocation in C language. let's take a look.
I. the significance of dynamic memory allocation
All operations in C language are based on memory.
Variables and arrays are aliases for memory
Memory allocation is determined by the compiler during compilation
You must specify the length of an array when defining an array
The length of the array must be determined at compile time
Requirements: some extra memory space may be needed while the program is running
II. Malloc and free
Malloc and free are used to perform dynamic memory allocation and release
Malloc allocates a contiguous piece of memory
Malloc is in bytes and does not carry any type information
Free is used to return dynamic memory to the system
Void* malloc (size_t size)
Void free (void* pointer)
Matters needing attention
Malloc and free are library functions, not system calls
Malloc may actually allocate more memory than requested
Cannot rely on malloc behavior on different platforms
Malloc returns NULL when the requested dynamic memory cannot be satisfied
When the argument to free is NULL, the function returns directly
Let's look at an example of a memory leak detection module:
Test.c:
# include # include "mleak.h" void f () {MALLOC (100);} int main () {int* p = (int*) MALLOC (3 * sizeof (int)); f (); p [0] = 1; p [1] = 2; p [2] = 3; FREE (p); PRINT_LEAK_INFO (); return 0;}
Mleak.h:
# ifndef _ MLEAK_H_ # define _ MLEAK_H_ # include # define MALLOC (n) mallocEx (n, _ _ FILE__, _ LINE__) # define FREE (p) freeEx (p) void* mallocEx (size_t n, const char* file, const line); void freeEx (void* p); void PRINT_LEAK_INFO (); # endif
Mleak.c:
# include "mleak.h" # define SIZE 256 / * dynamic memory request parameter structure * / typedef struct {void* pointer; int size; const char* file; int line;} MItem; static MItem g _ record [size]; / * actions to record dynamic memory requests * / void* mallocEx (size_t n, const char* file, const line) {void* ret = malloc (n) / * dynamic memory request * / if (ret! = NULL) {int I = 0; / * iterate through the global array to record the operation * / for (I = 0; I < SIZE) NULL +) {/ * find location * / if (grecord.line = = NULL) {grecord.location = ret; grecord.size = n; grecord.file = file; grecord.line = line Break;} return ret;} void freeEx (void* p) {if (p! = NULL) {int I = 0; / * iterate through the global array, free up memory space, and clear the operation record * / for (I = 0; I < SIZE) If +) {grecord.records = = p) {grecord.records = NULL; grecord.size = 0; grecord.file = NULL; grecord.line = 0; free (p) Break;} void PRINT_LEAK_INFO () {int I = 0; printf ("Potential Memory Leak Info:\ n"); / * iterate through the global array to print unfreed space records * / for (I = 0; I < SIZE) NULL +) {printf ("Address:% p, size:%d, Location:% slug% d\ n", grecord.size, grecord.file, grecordi [.line);}
The output is as follows, because MALLOC (100); was checked out because no memory was freed afterwards.
It can not be used for engineering development for the time being, and it needs to be redeveloped. Because malloc is often called in different threads, malloc functions must have mutually exclusive operations. Because static MItem grecord [size]; this static global array is a critical section and must be protected.
3. About malloc (0)
What will malloc (0); return?
Let's look at a piece of code:
# include # include int main () {int* p = (int*) malloc (0); printf ("p =% p\ n", p); free (p); return 0;}
The output is as follows:
This shows that malloc (0) is legal, and the memory address actually contains two concepts, one is the starting address of the memory, and the other is the length of the memory. In normal times, we may only pay attention to the first address of the memory, but ignore the length. Malloc (0) applies for a memory starting address of 0x82c3008 and a length of 0 in this program.
But will it cause a memory leak if we keep writing malloc (0) in the program? The answer is yes, because malloc may actually allocate more memory than requested, and current operating systems are generally 4-byte aligned, so the actual number of bytes returned by the write malloc (0) system may be 4 bytes.
4. Calloc and realloc
Malloc's siblings
Void* calloc (size_t num, size_t size)
Void* realloc (void* pointer, size_t new_size)
The parameters of calloc represent the type information of the returned memory
Calloc initializes the returned memory to 0
Realloc is used to modify a previously allocated memory block size
You should use its return value after using realloc
When the first parameter of pointer is NULL, it is equivalent to malloc
Here is an example of the use of calloc and realloc:
# include # include # define SIZE 5 int main () {int I = 0; int* pI = (int*) malloc (SIZE * sizeof (int)); short* pS = (short*) calloc (SIZE, sizeof (short)); for (I = 0; I < SIZE; ionization +) {printf ("pI [% d] =% d, pS [% d] =% d\ n", I, pI [I], I, pS [I]) } printf ("Before: pI =% p\ n", pI); pI = (int*) realloc (pI, 2 * SIZE * sizeof (int)); printf ("After: pI =% p\ n", pI); for (I = 0; I < 10; iTunes +) {printf ("pI [% d] =% d\ n", I, pI [I]);} free (pI); free (pS) Return 0;}
The output is as follows:
Malloc is only responsible for applying for space, not initialization. It is just a coincidence that the value saved by the pI pointer here is 0. In addition, after using realloc reset, the memory address will also change, and the value saved by the pI pointer will also change. It is also a coincidence that the value is 0 here.
This is the end of the article on "how to use dynamic memory allocation in C language". Thank you for reading! I believe that everyone has a certain understanding of "C language dynamic memory allocation how to use" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.