![]() |
![]() |
![]() |
![]() |
![]() |
This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
Allocate space for an array
#include <stdlib.h>
void* calloc ( size_t n,
size_t size );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The calloc() function allocates space from the heap for an array of n objects, each of size bytes, and initializes them to 0.
![]() |
A block of memory allocated with the calloc() function should be freed using the free() function. |
A pointer to the start of the allocated memory, or NULL if an error occurred (errno is set).
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char* buffer;
buffer = (char* )calloc( 80, sizeof(char) );
if( buffer == NULL ) {
printf( "Can't allocate memory for buffer!\n" );
return EXIT_FAILURE;
}
free( buffer );
return EXIT_SUCCESS;
}
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | No |
| Thread | Yes |
free(), malloc(), realloc(), sbrk()
![]() |
![]() |
![]() |
![]() |