Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C Memory management


May 11, 2021 C


Table of contents


C Memory management

This chapter explains dynamic memory management in C. T he C language provides several functions for memory allocation and management. These functions can be found in the header file of the .lt;stdlib.h>

Serial number Function and description
1 void *calloc(int num, int size);
This function assigns a Function Allocates an Array of ARRAY OF num An array of elements, the size of each element is size byte.
2 void free(void *address);
This function releases the H-memory block pointed to by Address.
3 void *malloc(int num);
This function assigns one num An array of bytes and initialize them.
4 void *realloc(void *address, int newsize);
This function reassigns memory and expands memory to newsize

Dynamically allocate memory

When programming, if you know the size of an array in advance, it is easier to define an array. For example, an array that stores a person's name can hold up to 100 characters, so you can define an array as follows:

char name[100];

However, if you do not know in advance the length of text that needs to be stored, for example, you store a detailed description of a topic to the store. Here, we need to define a pointer that points to characters that do not define the size of the memory learned, and then allocates the memory as needed, as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   char name[100];
   char *description;

   strcpy(name, "Zara Ali");

   /* 动态分配内存 */
   description = malloc( 200 * sizeof(char) );
   if( description == NULL )
   {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else
   {
      strcpy( description, "Zara ali a DPS student in class 10th");
   }
   printf("Name = %s\n", name );
   printf("Description: %s\n", description );
}

When the above code is compiled and executed, it produces the following results:

Name = Zara Ali
Description: Zara ali a DPS student in class 10th

The above program can also be written using calloc(), just replace the malloc with calloc, as follows:

calloc(200, sizeof(char));

When you allocate memory dynamically, you have full control and can pass values of any size. Arrays that are predefined in size cannot be changed once defined.

Resize and free up memory

When a program exits, the operating system automatically frees up all memory allocated to the program, but it is recommended that you call the function free() to free up memory when you do not need it.

Alternatively, you can increase or decrease the size of the allocated block of memory by calling the function realloc(). Let's look at the example above again using the realloc() and free() functions:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   char name[100];
   char *description;

   strcpy(name, "Zara Ali");

   /* 动态分配内存 */
   description = malloc( 30 * sizeof(char) );
   if( description == NULL )
   {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else
   {
      strcpy( description, "Zara ali a DPS student.");
   }
   /* 假设您想要存储更大的描述信息 */
   description = realloc( description, 100 * sizeof(char) );
   if( description == NULL )
   {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   }
   else
   {
      strcat( description, "She is in class 10th");
   }
   
   printf("Name = %s\n", name );
   printf("Description: %s\n", description );

   /* 使用 free() 函数释放内存 */
   free(description);
}

When the above code is compiled and executed, it produces the following results:

Name = Zara Ali
Description: Zara ali a DPS student.She is in class 10th

You can try not to reassign additional memory, and the strcat() function generates an error because there is not enough memory available when the description is stored.