Memory management in the C language

Source: Internet
Author: User

One: The memory is divided into 5 large areas

Stack area--store local variables

Heap area-Dynamically allocated memory during program operation

BSS zone-uninitialized global and static variables

Data segments--initialized global variables and static variables

Code Snippets-binary data generated by program compilation

Memory allocation Method:

1) is statically allocated, allocating memory int a = 10 in the compiled phase; Statically allocated memory is automatically freed after the program ends

2) Dynamic allocation, in the process of running the memory allocation, the dynamic allocation of memory is generally stored in the heap, the program ends after the memory is not automatically released, need to manually release

The C language library provides three functions for dynamically allocating memory:

1>malloc

1) malloc (length) continuous null for the length specified in the memory request

If successful: Returns the first address of the newly requested memory space

Failed: returned null

The return value of the malloc function is void * Type, so assigning a non-empty type requires a strong turn. such as (int *) malloc (the number of sizeof (int) * elements);

Code:

voidM_alloc () {//dynamically request 20 bytes of memory space, the first address of the memory space is placed in P    int*p = (int*) malloc (5*sizeof(int)); //determine if the assignment was successful    if(p!=NULL) {memset (void*) P,0,sizeof(p));  for(intI=0; i<5; i++) {printf ("%d\t", *p++); } printf ("\ n"); //if it succeeds, the data can be stored.*p =1; * (p+1) =Ten; * (p+2) = -; * (p+3) = +; * (p+4) =10000;  for(intI=0; i<5; i++) {printf ("%d\t", *p++); }            }Else{printf ("memory allocation failed! "); }}

Attention:

1) malloc () application space, if not initialized, storage is also the number of garbage

2) Initialize the memset (address, with what initialization, length);

2> calloc

Calloc (bis) First 4, apply several pieces of the second 4 each block of length 4 calloc (10,sizeof (int));

voidC_alloc () {//(void *) the size of 5 blocks per block    Char*p = (Char*) Calloc (5,sizeof(Char)); Char*p1=p; //Judging whether it is successful    if(p!=NULL) {                //for (int i=0; i<5; i++) {//printf ("%d\t", *p++); //        }printf ("\ n");  for(intI=0; i<5; i++,p++) {            *p= $+i; }                //*p = ' A '; //* (p+1) = 65; //* (p+2) = ' a '; //* (p+3) = ' Z '; //* (p+4) = ' x ';                 for(intI=0; i<5; i++) {printf ("%c\t", *p1++); }            }}

Space that is often used to request an array

Note: Calloc can help us automatically initialize to 0

3>realloc

ReAlloc (address, new length) after resizing, let the original pointer again point to P = (int *) realloc (p,100);

voidRe_alloc () {//(void *) the size of 5 blocks per block    Char*p = (Char*) Calloc (5,sizeof(Char)); Char*p1=p; //Judging whether it is successful    if(p!=NULL) {                 for(intI=0; i<5; i++,p++) {            *p= $+i; }        //A B C D E//after resizing, point back againP1= ReAlloc (P1,7*sizeof(Char)); * (p1+5)='a'; * (p1+6)='b';  for(intI=0; i<7; i++) {printf ("%c\t", *p1++); }            }

4) free ();

Why use Free ()

If you do not use free to release our own requested memory space, it may cause a memory leak

Free storage space

int *p = (int *) malloc (12);

Free (p); Release

*p = 10; (wrong) because P points to the memory space has been released so *p point to the memory space can not be assigned operation, generally we want to speak the pointer is null p = null;

Memory management in the C language

Related Article

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.