[2020.4.6] [Note] C dynamic memory allocation

C Dynamic Memory Allocation

An array is a collection of values fixed number, after the declared size of the array can not be changed. Sometimes, the array size may not be enough, we need dynamic expansion. Resolve this problem, you can manually allocate memory at runtime. This is referred to in C programming dynamic memory allocation .

Dynamic memory allocation functions are related to the library

  • malloc()

  • calloc()

  • realloc()

  • free()

These functions are <stdlib.h>defined in the header file.


1.malloc()

Name "malloc" represents the memory allocation, memory allocation.

The malloc()function of the reserved memory blocks specified number of bytes. Then, it returns a pointer to voidbe cast into any form of pointers.


malloc () syntax

ptr = (castType*) malloc(size);

Case

ptr = (int*) malloc(100 * sizeof(float)); 

The above statement is allocated 400 bytes of memory. This is because the floatsize of 4 bytes. Further, the pointer ptr first byte stored in the memory allocated memory address.

If you can not allocate memory, expression will produce a NULLpointer.


2.calloc()

Name "calloc" represents the continuous distribution, contiguous allocation.

malloc()Function allocates memory but does not initialize memory. The calloc() function allocates memory and initialized to zero for all bits.


calloc () syntax

ptr = (castType*)calloc(n, size);

Example:

ptr = (float*) calloc(25, sizeof(float)); 

The above statement is a floatcontinuous space type element 25 allocated in memory.


3.free()

Use calloc()or malloc()does not release the dynamically allocated memory created individually, you must explicitly use the free()free space.


free () syntax

free(ptr);

The statement released by the space allocated memory pointed ptr.


Example 1: malloc () and free ()

// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int)); // if memory cannot be allocated if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); // deallocating the memory free(ptr); return 0; } 

Here, we have a dynamic allocation of memory to n digits


Example 2: calloc () and free ()

// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) calloc(n, sizeof(int)); if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); free(ptr); return 0; } 

4.realloc()

If the dynamic allocation of memory shortage or exceed the requirements, you can use this realloc()feature to change the size of memory previously allocated.


realloc () syntax

ptr = realloc(ptr, x);

Here, ptr reassigned to the new size x.


Example 3: realloc ()

#include <stdio.h>
#include <stdlib.h> int main() { int *ptr, i , n1, n2; printf("Enter size: "); scanf("%d", &n1); ptr = (int*) malloc(n1 * sizeof(int)); printf("Addresses of previously allocated memory: "); for(i = 0; i < n1; ++i) printf("%u\n",ptr + i); printf("\nEnter the new size: "); scanf("%d", &n2); // rellocating the memory ptr = realloc(ptr, n2 * sizeof(int)); printf("Addresses of newly allocated memory: "); for(i = 0; i < n2; ++i) printf("%u\n", ptr + i); free(ptr); return 0; } 

When you run the program, the output is:

输入大小:2
先前分配的内存的地址:26855472
26855476

输入新的尺寸:4
新分配的内存地址:26855472
26855476
26855480
26855484

to sum up

malloc dynamic memory allocation, does not initialize

int n, *ptr = 0;
printf("Enter number of elements: ");
scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int)); 

If you can not allocate memory, NULL is returned

calloc dynamic memory allocation, initialization of all bit 0

int n, *ptr = 0;
printf("Enter number of elements: ");
scanf("%d", &n); ptr = (int*) calloc(n, sizeof(int)); 

If you can not allocate memory, NULL is returned

free release memory

free(ptr);

realloc reallocate memory

ptr = realloc(ptr, n * sizeof(int));

Guess you like

Origin www.cnblogs.com/fake8864/p/12640303.html