In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve dynamic memory allocation in C language. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Detailed explanation of dynamic memory allocation in C language
1. Why use dynamic memory allocation
Arrays can cause memory waste when they are used, and dynamic memory allocation can solve this problem.
2. Malloc and free
The C function library provides two functions, malloc and free, to perform dynamic memory allocation and release, respectively.
1) void * malloc (size_t size)
The parameter to malloc is the number of bytes of memory that need to be allocated. Malloc allocates a contiguous piece of memory. If the operating system cannot provide more memory to malloc, malloc returns a NULL pointer.
(2) void free (void * pointer)
The parameter to free is either NULL or a value previously returned from malloc, calloc, or realloc.
3. Calloc and realloc
1) void * calloc (size_t num_elements,size_t element_size)
Calloc is also used for memory allocation. The main difference between malloc and calloc is that the latter initializes a pointer to memory to 0 before returning it.
(2) realloc (void * ptr,size_t new_size)
The realloc function is used to modify the size of a previously allocated block of memory. If it is used to expand a memory, the original contents of the memory are retained, and the newly added memory is added to the back of the original memory block. If it is used to shrink a memory block, part of the memory at the end of the memory block is removed and the original contents of the rest of the memory are retained.
4. Use dynamically allocated memory
(1) use examples
Int * pi;pi = malloc (25 * sizeof (int)); if (pi = = NULL) {printf ("out of memery\ n"); exit (1);}
(2) use indirect access
Int * pi2,i;pi2 = pi;for (I = 0bot I < 25x I + = 1) * pi2++ = 0
You can also use subscript.
Int [I] for (I = 0 [I] < 25 [I] = 1) * Pi [I] = 0
5. Common dynamic memory errors
Common errors include dereferencing NULL pointers, crossing boundaries when operating on allocated memory, freeing memory that is not allocated, trying to free part of a dynamically allocated memory, and continuing to use a piece of dynamic memory after it is freed.
These are all the contents of the article "how to achieve dynamic memory allocation in C language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.