That year, I learned Linux C-talking about dynamic memory step by step

Source: Internet
Author: User


Reprinted please indicate the source: http://blog.csdn.net/muge0913/article/details/7314130


When using dynamic memory, you need to apply for resources and release resources by yourself. Users can allocate the required space at any time, allocate the space as needed, and release the requested memory at the end.

Dynamic Memory also has risks: it is complicated to manage the dynamic memory applied for in large projects, and it is hard to remember to release the applied memory. When releasing dynamic memory, there may be more than one pointer pointing to the memory, so it is easy to make an error when releasing it. Memory leakage occurs when the memory cannot be released, which is why the server restarts frequently for a period of time.

 

 

Memory Management Operations:

Memory Allocation Function:


#include <stdlib.h>void *malloc(size_t size)void *calloc(size_t nmemb,size_tsize)

The size in the malloc function is the size of the allocated memory, in bytes.

In the calloc function, size indicates the data item size, and nmemb indicates the number of data items. Therefore, the allocated memory size is size * nmemb

The biggest difference between malloc and calloc is that calloc initializes the applied inner output to 0.

If the call succeeds, a pointer to the allocated memory is returned. If the call fails, null is returned.


Memory adjustment:

For realloc (), the function prototype is void * realloc (void * PTR, size_t size), which changes the size of the memory area referred to by PTR to the size length, the size can be greater than or less than the size of the original dynamic memory. realloc usually adjusts the size of the dynamic memory based on the original data, and the original data content remains unchanged. When the size is greater than the original data and cannot be adjusted in the original location, realloc will re-open the memory and copy the original data to this. If the reallocation succeeds, the pointer pointing to the allocated memory is returned. Otherwise, the NULL pointer is returned. When the memory is no longer used, use the free () function to release the memory block. Note: When the memory is allocated successfully, the original pointer PTR = NULL should be used. Otherwise, a wild pointer will be formed, which may cause system crash. Whether the above method applies for memory, after applying for memory, it will eventually use free to release space, otherwise it will cause memory leakage.

If PTR is null, realloc is equivalent to malloc, And if size = 0, it is equivalent to free.


Memory release:

#include<stdlib.h>voidfree(void *pr);

Free is used to release dynamic memory with malloc or calloc requests. An error occurs when the pointer is used again after the memory is released.

 

Example:

#include <stdio.h>#include <stdlib.h> char *alloc_test(); main(){     char*p1,*p2;     p1= alloc_test();     p2= p1;     printf("%s\n",p1);     printf("%s\n",p2);     free(p1);     //free(p2);}char *alloc_test(){     char*pchar = malloc(20);     strcpy(pchar,"helloalloc_test");     returnpchar;}

Free (P2); comment out

Otherwise, an error occurs, indicating that P1 and P2 point to a memory at the same time, and the memory block is released through P1. Of course, an error occurs when the memory is released again through P2.

Pchar also points to the memory block, but the memory is released after P1, because after alloc_test is called, pchar is released when the memory block is not released.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.