Article title: Linux-based C programming and kernel guidance. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Dynamic memory usage
Although the method in the previous section can avoid overflow, it will lead to data loss. next we will learn a better method-dynamic memory usage. Because the dynamic memory is allocated and used by the user, some system calls are required. next we will learn about them separately.
First, we need a system that calls the calloc () function for dynamic memory allocation. its prototype is:
# Include Void * malloc (size_t size); void * calloc (size_t nmemb, size_t size ); |
Both the malloc and calloc functions are used to allocate dynamic memory space. The size parameter in malloc indicates the size of the memory space to be allocated, in bytes; the calloc parameter nmemb indicates the number of data items occupied by memory allocation. the parameter size indicates the size of each data item, in bytes. Therefore, the calloc function allocates a memory size of nmemb * size.
The biggest difference between calloc and malloc is that the calloc function initializes the allocated memory space and sets all locations to 0.
When the call is successful, their return values are pointers to the allocated memory space. if the call fails, the return value is NULL.
When a dynamic memory is used up, you need to manually release it. The system call used is the free () function, and its prototype is:
# Include Void free (void * ptr ); |
The ptr parameter is a pointer to the dynamic memory to be released. release the dynamic memory after it is used to avoid memory leakage. The following is an example of dynamic memory management.
Program 4.2 is as follows:
# Include # Include Char * upcase (char * inputstring); int main (void) {char * str1; str1 = upcase ("Everybody");/* call the Subfunction upcase () */printf ("str1 = % s \ n", str1); free (str1);/* release memory */return 0;} char * upcase (char * inputstring) {char * newstring; int counter, N; N = strlen (inputstring);/* N is the string length * // * apply for N + 1 bytes of memory space, if an error occurs, the system reports an error and exits */if (! (Newstring = malloc (N + 1) {printf ("error allocating memory! \ N "); exit (255);}/* copy the original string to the newly applied memory block */strcpy (newstring, inputstring); for (counter = 0; counter {If (newstring [counter]> = 97 & newstring [counter] <= 122) newstring [counter]-= 32; /* convert lowercase letters to uppercase letters */} return newstring ;} |
Result analysis:
In this program, because dynamic memory is used, the program can return the pointer of the memory space allocated in the Subfunction to the main function. At the same time, the dynamic memory is used, so that the sub-function can flexibly allocate the required memory space (Note: To apply for one more byte space, it is because when strlen calculates the string length, it does not include the "\ 0" sign at the end, but requires space for the end mark of the string when copying the string ). The running result of this program is: EVERYBODY
Note: In this program, the Subfunction can use N = strlen (inputstring) because inputstring is a definite string. In the 4.1 program in the previous section, newstring is an uninitialized character array, so strlen cannot be used for length.