Zhou Ligong "Programming and Data Structure": Deep Anatomy of Dynamically Distributed Memory Free() Functions and Realloc()
Professor Zhou Ligong's years of hard work "Programming and Data Structure", after the publication of the book content, set off a learning boom in the electronics industry. Authorized by Professor Zhou Ligong, this public number has serialized the contents of this book, and is willing to share it.
The first chapter is the basis of programming. This article is 1.9.3 free() function and 1.9.4 realloc() function.
> > > > Â 1.9.3 Â Free() function
For programs, memory blocks that are no longer accessible are called garbage, and programs that leave garbage have memory leaks. Although some languages ​​provide garbage collectors for automatic location and recycling of garbage, C is not available. Each program is required to be responsible for reclaiming its own garbage by calling the free() function to release the unneeded memory.
Usually malloc() is used with free(). When the dynamic memory is used up, if it is not released in time, it will inevitably lead to "memory leak (ie, memory space reduction)", which will affect the normal operation of the program. The free() function prototype that frees memory is as follows:
Void free(void *pointer);
The pointer pointed to by malloc() is passed as a parameter to free() to free the memory. Although the free() function allows you to reclaim memory that is no longer needed, using this function causes a new problem: dangling pointers. Although calling the free(pi) function will release pi, it will not change the pi itself. If you forget that pi no longer points to a valid memory block, then chaos may come soon:
Char *pi = malloc(5);
Free(pi);
Strcpy(pi, "abc"); // error
That is, modifying the memory pointed to by pi is a serious error because the program loses control of this memory. In fact, dangling pointers are hard to find because several pointers may point to the same memory block. After the memory block is freed, all pointers are left floating. With the free() function, malloc() can also be used to allocate a contiguous memory space at runtime to achieve the purpose of changing the size of the array. such as:
Char * pi = malloc(5);
That is, the variable pi points to 5 consecutive bytes that have been allocated in the heap, as if an array of 5 characters was declared. Obviously, a dynamic array is an array that is allocated on the "heap" and referenced by a pointer variable. The steps to assign a dynamic array are as follows:
â— Declare a pointer variable to hold the address of the first element of the array variable;
â— Â Call malloc() to allocate memory for elements in an array variable
â— Assign the result of malloc() to the pointer variable.
Since the memory size occupied by different data types is different, the size is the number of array variable elements multiplied by the size of the memory occupied by each element. For example, an array variable with 5 int elements needs to allocate memory. such as:
Int *pi = malloc(5 * sizeof(int));
Unlike arrays, memory must be freed when not in use. such as:
Free(pi);
If you need 10 elements to use, you should first release the original memory, and then apply for new memory. such as:
Free(pi);
Pi = malloc(10 * sizeof(int));
Obviously, the data stored in the original memory is gone. In order to retain the original data, you need to do some more work:
Int *temp = pi; // Â Let temp point to the original memory
Pi = malloc(10 * sizeof(int)); // Â Let pi point to new memory
Memcpy(pi, temp, 5 * sizeof(int)); // Â Copy the original memory data to the new memory
Free(temp); // Â Release the original memory
But the above work can be done with just one statement, such as:
Pi = realloc(pi, 10*sizeof(int));
Since the free function does not check if the incoming pointer is NULL, it will not set the pointer to NULL before returning, so the programmer will create its own free function. The interface and implementation of the saferfree function are shown in Listing 1.49 and the program listing. 1.50.
Listing 1.49 interface to the saferfree() function (saferFree.h)
1 #pragma once
2 void saferFree(void **pp);
Listing 1.50 Implementation of the saferfreeh function interface (saferFree.c)
1 #include
2 #include
3
4 void saferFree(void **pp)
5 {
6 if(pp != NULL && *pp != NULL)
7 free(*pp);
8 *pp = NULL;
9 }
10 }
If you call the saferFree function with the saferFree macro, you can omit the type conversion and pass the address of the pointer. which is:
#define NewSaferFree(P) saferFree((void **)&p)
The form of its call is as follows:
Int *pi = malloc(sizeof(int));
NewSaferFree(pi);
> > > Â 1.9.4 realloc() function
Alloc is an abbreviation for allocate allocation, and the prefix re is the meaning of reassignment. If there is any remaining memory behind the original memory, realloc() only modifies the allocation table or returns the address of the original memory; if there is no memory remaining, realloc() will apply for new memory and then copy the original memory data to the new memory. The original memory will be freed () and realloc() will return the address of the new memory. The prototype of the realloc() function is as follows:
Void *realloc(void *pointer, unsigned int size);
When the realloc() function is called, point must point to a block of memory previously obtained through a call to malloc, calloc, or realloc. Size indicates the size of the newly allocated memory, in bytes. Its role is to change the size of the dynamic space pointed by the pointer to size, and the value of the pointer does not change. If the reassignment is unsuccessful, it returns NULL; if you have obtained dynamic space through malloc() and don't want to change its size, you can use realloc() to reassign it.
EQM(ECM)
EQM series motors are designed with advanced electronic control technology, make the motor efficiency greatly increased,the external structure design of EQM series motors will maintain the similar external structure design as YZF series motors,motor accessories such as fan blades, rings or grids.brackets for EQM motors are the same as YZF series`. therefore EQM series motors can completely replace the YZF series motors without any other changes.Comparing with YZF series motors, EQM motors display an obvious advantage in energy saving. saving up to 70%.It can greatly reduce the electricity cost for motor operation and carbon dioxide emissions Not only that.due to the heating of EQM series motors themselves are very low.it would lead to the entire
refrigeration system works more efficient and make motor running more stable and reliable
Centrifugal Fan,Centrifugal Blower,Centrifugal Blower Fan,Centrifugal Air Blower
Hangzhou Jinjiu Electric Appliance Co Ltd. , https://www.jinjiufanmotor.com