T_MALLOC convenience macro to allocate memory
ptr
pointer variable to assign allocated memory to
size
number of bytes to allocate
type
data type to cast allocated memory to
None
None
T_MALLOC uses TutMalloc to allocate memory, assigns the new memory to the pointer variable ptr
, and checks for errors (TutMalloc returning NULL
). Because TutMalloc 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_MALLOC can be used in most C contexts. T_MALLOC cannot be used in an expression.
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 0
’s placed 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 and most of the fields are being filled with actual data, it may be faster to use T_MALLOC and then explicitly clear the necessary fields in the new structure.
T_CALLOC, T_FREE, T_REALLOC, T_STRDUP
This example allocates space for a string:
TIBCO SmartSockets™ Utilities Software Release 6.8, July 2006 Copyright © TIBCO Software Inc. All rights reserved www.tibco.com |