T_CALLOC


Name

T_CALLOC — convenience macro to allocate and clear memory

Synopsis

void T_CALLOC(ptr, size, type) 
type ptr; 
int size; 

Arguments

ptr — pointer variable to assign allocated memory to

size — number of bytes to allocate

type — data type to cast allocated memory to

Return Values

None

Diagnostics

None

Description

T_CALLOC uses TutCalloc to allocate memory, assigns the new memory to the pointer variable ptr, and checks for errors (TutCalloc returning NULL). Because TutCalloc returns a (T_PTR) pointer, type is used to cast the new memory to the appropriate type (to make the intention of the code clearer).

T_CALLOC can be used in most C contexts. T_CALLOC cannot be used in an expression.

Caution

type is used to build a cast and should not have parentheses around it.

The library function malloc is significantly faster than calloc because it does not have to clear the newly allocated memory. Use T_CALLOC instead of T_MALLOC if you need to put 0s in all bytes of the new memory. For example, a common use of T_CALLOC is to allocate storage for a pointer to a structure. If many of these structures are being allocated one at a time, it may be faster to use T_MALLOC and then explicitly clear the necessary fields in the new structure.

See Also

T_FREE, T_MALLOC, T_REALLOC, T_STRDUP

Examples

This example allocates memory:

char *char_ptr; 
T_CALLOC (char_ptr, sizeof (char), char); 

TIBCO SmartSockets™ Utilities
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com