Linux under C programming: talking about dynamic memory

Source: Internet
Author: User
Tags function prototype printf linux

Using dynamic memory requires users to request resources and release resources themselves. The user can allocate the required space at any time, allocate the space size as needed, and release the application memory at the end.

Dynamic memory is also a problem: managing the dynamic memory requested in a large project is complex, and it is hard to recall the memory of the application being released. There may be more than one pointer to the memory when releasing dynamic memory, so it is easy to make mistakes when releasing. The inability to release memory can result in memory leaks, which is why the server reboots frequently every time.

Memory Management operations:

Allocating memory functions:

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

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

The size of the function calloc is a data item, NMEMB is the number of data items. So allocate memory size to SIZE*NMEMB

The biggest difference between malloc and calloc is that Calloc will initialize the application into 0.

A successful call returns a pointer to the allocated memory, which returns NULL if the call fails

Memory Adjustment:

For ReAlloc (), the function prototype is the void* realloc (void *ptr,size_t size), which changes the size of the memory area of the PTR to be length of size, and the size can be greater than or smaller than the original dynamic memory. ReAlloc is usually based on the original data to adjust the size of the dynamic memory is the original data content unchanged. When the size is larger than the original data and the original position cannot be adjusted, the realloc will reopen the memory and copy the original data here. Returns a pointer to the allocated memory if the reallocation succeeds, otherwise null pointers are returned. When memory is no longer in use, you should use the free () function to release the memory block. One thing to note: When allocating memory successfully, the original pointer should be ptr=null, otherwise the wild pointer will be formed, which may cause system crashes. Regardless of the way in which you apply for memory, after the application of memory, you will eventually have to free space, otherwise it can cause memory leaks.

ReAlloc is equivalent to malloc if PTR is null, if size=0 is equivalent to free

Release of Memory:

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

Free is used to release dynamic memory with malloc or calloc requests. The use of the pointer after memory release will cause an error.

Examples are as follows:

#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 ();     
         
     strcpy (Pchar, "helloalloc_test");     
         
     Returnpchar;     
         
}

To put free (p2), comment out

Otherwise there will be an error, thought that P1,P2 also points to a memory, through the P1 released the memory block, when through the P2 again released memory of course, error.

Pchar also points to the memory block, but also p1 the freed memory, because after the call Alloc_test the Pchar is released when the memory block is freed.

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.