Show: Object Pascal C++
Display Preferences

calloc

From Appmethod Topics
Jump to: navigation, search

Go Up to alloc.h Index


Header File

alloc.h, stdlib.h

Prototype

void *calloc(size_t nitems, size_t size);

Description

Allocates main memory.

calloc provides access to the C memory heap. The heap is available for dynamic allocation of variable-sized blocks of memory. Many data structures, such as trees and lists, naturally employ heap memory allocation.

calloc allocates a block of size nitems * size. The block is initialized to 0.

Return Value

calloc returns a pointer to the newly allocated block. If not enough space exists for the new block or if nitems or size is 0, calloc returns NULL.

Portability

POSIX ANSI C ANSI C++ Win32 Win64 OS X
calloc + + + +

Example

#include <stdio.h>
#include <alloc.h>
#include <string.h>

int main(void) {
    char *str = NULL;
    /* allocate memory for string */
    str = (char *) calloc(10, sizeof(char));
    /* copy "Hello" into string */
    strcpy(str, "Hello");
    /* display string */
    printf("String is %s\n", str);
    /* free memory */
    free(str);
    return 0;
}
Personal tools
In other languages