T_MALLOC


Name

T_MALLOC — convenience macro to allocate memory

Synopsis

void T_MALLOC(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_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.

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 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.

See Also

T_CALLOC, T_FREE, T_REALLOC, T_STRDUP

Examples

This example allocates space for a string:

T_STR string; 
 
T_MALLOC(string, 80, T_STR); 
strcpy(string, "hi there"); 

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