Knowledge points and examples of dynamic memory allocation in C language

Time:2021-9-17

How to realize dynamic memory allocation in C language

We often open up memory space for programs in advance, and then operate.


int arr[5] ;

When defining this array, we must open up space in advance, and the opened memory space always exists in the process of program execution. The space will not be released until the function is executed. One problem is that this array cannot be modified in the program.

These problems make us inconvenient to use. Therefore, malloc () function is provided in C.

As for malloc () function, this function accepts one parameter: the number of bytes of memory required. Then malloc () finds the appropriate size block in available memory. In this process, malloc () can be used to return the address of the first byte of that memory. Therefore, it means that we can use pointers to operate. Malloc () can be used to return array pointers, structure pointers, etc., so we need to assign the type of the return value to the appropriate type. Malloc () returns a null pointer when it cannot find the required space.

Example:


double *p;

p=(double*)malloc(30*sizeof(double));

In this program, we first open up 30 double type spaces, and then point P to the location of this space. The pointer here points to the first double value. Not all of the 30 double spaces we have opened up. This is the same as the array. The pointer pointing to the array points to the address of the first element of the array, not the element of the whole array. Therefore, our operation here is the same as that of array,

P [0] is the first element and P [1] is the second element.

At this point, we can master a method to declare dynamic arrays.

int arr[n];

p=(int *)malloc(n*sizeof(int));

//When we use it here, we need to multiply the number of elements by the length of type bytes, so as to dynamically open up memory space.

When we use malloc () to open up the memory space, what we need to consider is to free the memory space. Here, C provides us with the free () function. The free () parameter is the address returned by the malloc () function, freeing up the space opened up by the previous malloc () function.

Example:

Release the space we have opened up above, so we can do this


free(p);

The program also calls the exit () function, which ends the program when memory allocation fails.

Program example:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#The include < malloc. H > // malloc() function is included in malloc. H
#include<stdlib.h>
int main(void)
{
  char*a = NULL;// Declare a pointer to the char * type of A
  a = (char*)malloc(100 * sizeof(char));// Use malloc to allocate the first address of memory, and then assign it to a
  If (! A) // if malloc fails, you can get some logs
  {
    perror("malloc");
    return-1;
  }
  sprintf(a, "%s", "HelloWorld\n");// "HelloWorld \ n" writes the address a points to
  printf("%s\n", a);// Output user input data
  free(a);// Free up used memory address
  system("pause");
  return 0;// Example 2 is there a memory leak?
}

This program is mainly used to detect the error of malloc return value condition.

Here we need to note that in C, type assignment (char *) is optional, but it is necessary in C + +, so using type assignment will make it easier to migrate C programs to C + +.

The use of dynamic arrays is mainly to obtain the flexibility of the program. We can make the array open up as many elements as we need. No need to waste space.

Supplement of knowledge points:

Common dynamic memory errors

Common errors include: dereferencing a null pointer, crossing a boundary when operating on allocated memory, freeing memory that is not allocated memory, trying to release part of a dynamically allocated memory, and continuing to use a dynamically allocated memory after it is released.

This is the end of this article about the knowledge points and examples of dynamic memory allocation in C language. For more information about how to realize dynamic memory allocation in C language, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!