A function that allocates memory space in C programming

  • 2020-04-02 03:16:35
  • OfStack

C language malloc() function: dynamic allocation of memory space
The header file:


#include <stdlib.h>

The malloc() function is used to dynamically allocate memory space (if you don't know about dynamic memory allocation, see: C dynamic memory allocation and variable storage category). The prototype is:


void* malloc (size_t size);

Size is the amount of memory space to be allocated, in bytes.

Malloc () allocates a specified amount of memory space in the heap to hold data. This memory space is not initialized after the function completes, and their value is unknown. If you want to initialize while allocating memory, use the calloc() function.

A successful allocation returns the address to the memory, and a failure returns NULL.

Since you may or may not have memory space when you apply for it, you need to make your own judgment on whether the application is successful before you proceed.

If the value of size is 0, the return value will vary depending on the standard library implementation, may or may not be NULL, but the returned pointer should not be referenced again.

Note: the return value type of the function is void *. Therefore, when using malloc(), we usually need to cast a void pointer to the type we want, for example:


char *ptr = (char *)malloc(10); //Allocate 10 bytes of memory space to hold characters

Example of dynamic memory allocation:


#include <stdio.h> 
#include <stdlib.h> 
int main ()
{
 int i,n;
 char * buffer;
 printf (" Length of input string: ");
 scanf ("%d", &i);
 buffer = (char*)malloc(i+1); //The string ends with 0
 if(buffer==NULL) exit(1); //Determine whether the assignment is successful
 //Randomly generated string
 for(n=0; n<i; n++)
  buffer[n] = rand()%26+'a';
 buffer[i]='0';
 printf (" The randomly generated string is: %sn",buffer);
 free(buffer); //Free memory space
 system("pause");
 return 0;
}

Operation results:


 Length of input string: 20
 The randomly generated string is: phqghumeaylnlfdxfirc

C calloc() function: allocates memory space and initializes it
The header file:


#include <stdlib.h>

The calloc() function is used to dynamically allocate memory space and initialize it to 0. Its prototype is:


 void* calloc (size_t num, size_t size);

Calloc () dynamically allocates num contiguous Spaces of size length in memory and initializes each byte to 0. So the result is that num*size is allocated in bytes of memory, and each byte has a value of 0.

A successful allocation returns the address to the memory, and a failure returns NULL.

If the value of size is 0, the return value will vary depending on the standard library implementation, may or may not be NULL, but the returned pointer should not be referenced again.

Note: the return value type of the function is void *. Therefore, when using calloc(), we usually need to cast a void pointer to the type we want, for example:


char *ptr = (char *)calloc(10, 10); //Allocate 100 bytes of memory space

An important difference between calloc() and malloc() is that calloc() automatically initializes the memory space to zero after dynamically allocating the memory, whereas malloc() does not initialize the memory space, and the data inside is unknown junk data. The following two ways are equivalent:


//Calloc () allocates memory space and initializes it
char *str1 = (char *)calloc(10, 2);
//Malloc () allocates memory space and initializes memset()
char *str2 = (char *)malloc(20);
memset(str2, 0, 20);

Code example:


#include <stdio.h>
#include <stdlib.h>
int main ()
{
 int i,n;
 int * pData;
 printf (" Number of Numbers to enter: ");
 scanf ("%d",&i);
 pData = (int*) calloc (i,sizeof(int));
 if (pData==NULL) exit (1);
 for (n=0;n<i;n++)
 {
  printf (" Please enter a number  #%d : ",n+1);
  scanf ("%d",&pData[n]);
 }
 printf (" The number you entered is: ");
 for (n=0;n<i;n++) printf ("%d ",pData[n]);
 
 free (pData);
 system("pause");
 return 0;
}

Operation results:


 Number of Numbers to enter: 4
 Please enter a number  #1 : 126
 Please enter a number  #2 : 343
 Please enter a number  #3 : 45
 Please enter a number  #4 : 234
 The number you entered is: 126 343 45 234

The above program will store the number you enter and then output it. Because memory is allocated dynamically to your needs while the program is running, you can enter different Numbers each time you run the program.


Related articles: