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 use dynamic memory allocation function in C language

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use C language dynamic memory allocation function". Xiaobian shows you the operation process through actual cases. The operation method is simple and fast, and the practicality is strong. I hope this article "how to use C language dynamic memory allocation function" can help you solve the problem.

Parameters of local variables and functions apply for space in the stack area

Global variables and static variables apply space to static zones

Dynamic memory allocation applies space to the heap (located in or header file)

1. malloc

void* malloc (size_t size);

allocate memory block

Allocates a contiguous block of available bytes and returns a pointer to the beginning of the block.

The contents of the newly allocated memory block are not initialized and the data in the memory block is indeterminate.

If the is parameter is zero, the return value depends on the specific library implementation (it may or may not be a null pointer).

parameters

The size of the memory block, in bytes.

is an unsigned integer type, size_t.

return value

On success, is a pointer to the memory block allocated for the function.

This pointer is always of type void* and can be converted to the desired data pointer type. (C++ must cast due to its stricter type checking)

If the function fails to allocate the requested memory block, a null pointer NULL is returned.

#include#include#include#includeint main(){ int* p = (int*)malloc(10 * sizeof(int)); if (p == NULL) { printf("%s\n", strerror(errno)); } else { for (int i = 0; i < 10; ++i) { *(p + i) = i; } for (int i = 0; i < 10; ++i) { printf("%d ", *(p + i)); } } free(p); p = NULL; /* 1. Disconnect the pointer from the dynamically opened space to avoid dangerous pointer operations 2. Prevent duplicate deallocation of memory space in the same dynamic space */ return 0;}

output

0 1 2 3 4 5 6 7 8 9

2. free(used to free up dynamically opened space)

void free(void* ptr);

deallocation of memory blocks

1. If the space pointed to by ptr is not dynamically opened, then the behavior of the free function is undefined.

2. If ptr is a NULL pointer, the free function does nothing.

Free only frees up heap space, but ptr still points to that space. So after using free to set ptr to NULL, cut ptr and the memory block connection.

parameters

Pointer to the space to be freed (must point to the initial position)

return value

no

Error Cases

#include#includeint main(){ int* p = (int*)malloc(sizeof(int) * 10); if (p == NULL) { return 1; } for (int i = 0; i < 10; ++i) { *(p + i) = i; } for (int i = 0; i < 10; ++i) { printf("%d ", *(p++));//here pointer moves } free(p);//pointer that causes free to be released is not the initial position, the program crashes p = NULL; return 0;} 3. calloc

void* calloc(size_t num,size_t num);

Allocate and zero initialize memory blocks

1. The function opens up space for num elements of size.

2. The only difference from malloc is that calloc initializes each letter of the requested space to 0 before returning the address.

#include /* printf, scanf, NULL */#include /* calloc, exit, free */int main (){ int i,n; int * pData; printf ("Amount of numbers to be entered: "); scanf ("%d",&i); pData = (int*) calloc (i,sizeof(int)); if (pData==NULL) exit (1); for (n=0;n

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