In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "C language dynamic array detailed introduction". In daily operation, I believe many people have doubts about the detailed introduction of C language dynamic array. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts of "C language dynamic array detailed introduction"! Next, please follow the small series to learn together!
directory
memory allocation function malloc calloc realloc free
Memset memcpy memmove
Creation and Release of Two-dimensional Dynamic Array
memory allocation function malloc calloc realloc free heap memory allocation function description void * malloc(int n) parameter n is the number of bytes required to be allocated. Note that the memory allocated by the malloc function is uninitialized. The memset function must be used to initialize. calloc(10, sizeof(char)); Two arguments: number of units, size of unit. Allocate memory and clean up memory. initial contents are all 0;realloc(p,10); resize memory; allocated memory space is not initialized; use the newly returned address, free(void * p) to free space on the heap pointed to by pointer p;
malloc() does not initialize the allocated memory space, and the original values in memory are not cleaned up.
realloc(), on the basis of the original memory, add continuous memory in the middle of the heap, if the original memory has no continuous space to expand, allocate a new space, copy the original memory to the new space, and then release the original memory.
p = malloc(10); //Apply directly for 4K space. Memory page size. The return value is an untyped pointer void *, which can be cast. free(p);char *p = calloc(10, sizeof(char)); //two parameters: number of units, size of unit. char *p2 = realloc(p,10); //Add contiguous memory in the middle of the heap on top of the original memory. If the original memory has no contiguous space to expand, allocate a new space, copy the original memory to the new space, and then release the original memory. char *p2 = realloc(NULL,20); //is equivalent to malloc
Realloc, like malloc, allocates memory only, does not clean up, and needs to be initialized manually.
//complex data memory in heap
memset memcpy memmovemeset (buf,0,sizeof(buf));//The three parameters are: the memory address to be set, the value to be set, and the number of bytes of memory occupied by the value. Initialize a block of memory to 0. extern void *memset(void *buffer, int c, int count) memcpy(void *dest,void *src,unsigned int count);//Copy all the memory contents of buffer 1 address to buffer 2, copy size is the third parameter: bytes must ensure that there is no overlap of memory regions. memmove(buf2, buf1, sizeof(buf1));//Memory moves without changing the original memory value. Creation and Release of Two-dimensional Dynamic Array
In the process of creating a dynamic array, we have to follow a principle, that is, when creating it, we create it layer by layer from the outer layer to the inner layer;
And release time from the inner layer to the outer layer, layer by layer release.
int main(){ int num1,num2; num1 = 4; num2 = 5; int **array = (int **)calloc(num1,sizeof(int *));//open space, num1 one-dimensional int* pointer for(int i=0;i
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.