Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize dynamic memory allocation in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to implement dynamic memory allocation in C language. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

C program, different data in memory allocation instructions

1) Global variables-static storage areas in memory

2) Non-static local variables--dynamic storage area in memory--stack stack

3) temporary use of data-the establishment of dynamic memory allocation area, open at any time when needed, do not need to release in time-heap heap

4) Apply to the system for the required size of space according to needs. Since it is not defined as a variable or array in the declaration part, these data cannot be referenced by variable name or array name, but can only be referenced by pointer)

Correlation Function of Dynamic Memory Allocation

The header #include declares four functions for dynamic memory allocation.

void * malloc (usigned int size)//memory allocation

Function-Allocates a contiguous space of size in the dynamic storage area (heap area) of memory.

The parameter size is of type unsigned integer, and the function returns the address of the first byte of the allocated field, i.e., the function is a pointer function that returns a pointer to the beginning of the allocated field.

malloc(100); opens up temporary space of 100 bytes and returns the address of its first byte

void *calloc (unsigned n,unsigned size)

Function-allocates n contiguous spaces of size in the dynamic storage area of memory. This space is generally large enough to hold an array. The calloc function can open up dynamic storage space for a one-dimensional array. n is the number of elements in the array, and the length of each element is size. The function returns a pointer to the starting position of the allocated field; if the allocation is unsuccessful, NULL is returned. p = calloc(50, 4); //Create 50*4 bytes of temporary space and assign the starting address to the pointer variable p

4) Function prototype: void free (void *p)

Action-frees the dynamic space pointed to by variable p so that it can be reused by other variables. p is the last calloc or malloc function call returned free function no return free (p ); //frees the allocated dynamic space pointed to by p

5) function prototype void *realloc (void *p, unsigned int size)

Function--reallocate the size of dynamic space obtained by malloc or calloc function, change the size of dynamic space pointed to by p to size, the value of p does not change, allocation failure returns NULLrealloc(p, 50); //change the allocated dynamic space pointed to by p to 50 bytes

6) Return type description

application example

Dynamically create an array, input the scores of 5 students, another function detects scores below 60 points, and outputs unqualified scores.

code demonstrates

#include #include int main() { void check(int *); int * p,i; //open up a 5 * 4 space in the heap and convert the address (void *) to (int *) , assign it to p p = (int *)malloc(5*sizeof(int)); for( i = 0; i < 5; i++) { scanf("%d", p + i); } check(p); // free(p); //Destroy the space pointed to by heap p getchar(); getchar(); return 0;}void check(int *p) { int i; printf("\n Failed grades are: "); for(i =0; i < 5; i++) { if(p[i] < 60) { printf(" %d ", p[i]); } }} Basic principles of dynamic memory allocation

1) Avoid allocating large amounts of small memory blocks. Allocating memory on the heap has some overhead, so allocating many small memory blocks is more expensive than allocating a few large memory blocks

2) Allocate memory only when needed. As long as the memory block on the heap is used up, it needs to be released in time (if dynamic memory allocation is used, the principle needs to be observed: who allocates, who releases), otherwise memory leaks may occur.

3) Always make sure to free up memory to allocate. When writing code to allocate memory, determine where in the code memory is to be freed

4) Before freeing memory, make sure you do not inadvertently overwrite allocated memory addresses on the heap, otherwise the program will have a memory leak. Be careful when allocating memory in a loop

5) List of pointer usage

Thank you for reading! About "C language how to achieve dynamic memory allocation" This article is shared here, I hope the above content can have some help for everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report