Latest web development tutorials

C Memory Management

This chapter will explain the C dynamic memory management. C language for the allocation and management of memory provides several functions. These functions can be found in<stdlib.h> header file.

序号函数和描述
1void *calloc(int num, int size);
该函数分配一个带有num个元素的数组,每个元素的大小为size字节。
2void free(void *address);
该函数释放 address 所指向的h内存块。
3void *malloc(int num);
该函数分配一个num字节的数组,并把它们进行初始化。
4void *realloc(void *address, int newsize);
该函数重新分配内存,把内存扩展到newsize

Dynamic memory allocation

When programming, if you know in advance the size of the array, the array definition easier. For example, an array to store names, which accommodate up to 100 characters, so you can define the array, as follows:

char name[100];

However, if you do not know in advance the length of the text to be stored, such as a detailed description of a topic related to your store. Here, we need to define a pointer that points to a character undefined learned memory size, and then the follow-up to allocate memory on demand, 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 usecalloc () to write, just need to replace malloc calloc, as shown below:

calloc(200, sizeof(char));

When the dynamic allocation of memory, you have complete control over the size of any value can be passed. Those pre-defined array size, the size can not be changed once it is defined.

Re-adjust the memory size and free memory

When the program exits, the operating system automatically frees all memory allocated to the program, however, recommend that you do not need memory, you should call the functionfree () to free memory.

Alternatively, you can call the functionrealloc () to increase or decrease the size of the allocated memory block.Let's use realloc () and free () function, look again at the example above:

#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 not try to re-allocate extra memory, strcat () function generates an error because of insufficient stored description available memory.