Linux C library functions Encyclopedia: Memory Control functions

Source: Internet
Author: User
Tags printf linux

1.calloc (Configure memory space)

Related functions:

Malloc,free,realloc,brk

Table header file:

#include <stdlib.h>

To define a function:

void *calloc (size_t nmemb,size_t size);

Function Description:

Calloc () is used to configure the NMEMB contiguous unit of memory, each unit of size, and returns a pointer to the first element. This works the same way as in the following ways: malloc (nmemb*size), but the memory content is initialized to 0 when the calloc () is used to configure memory.

return value:

If the configuration succeeds, a pointer is returned and NULL is returned if it fails.

Example one:

#include <stdio.h> 
#include <stdlib.h> 
int main (void) 
    { 
        int i; 
        int *pn= (int *) calloc (10,sizeof (int)); 
        for (i=0;i<10;i++) 
        printf ("%3d", *pn++); 
        printf ("\ n"); 
        Free (PN); 
        return 0; 
    }

Case TWO:

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
int main (void)  
{  
    char *str = NULL;  
/* Allocate memory space/
   char  *str = (char*) calloc (sizeof (char));  
/* Write Hello to * *
    strcpy (str, "Hello");  
http://www.bianceng.cn
/* Display variable content
    /printf ("String is%s\n", str);  
/* Free Space * *
    (str);  
    return 0;  
}

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.