malloc and free | Standard C Library Functions | Teradata Vantage - 17.10 - malloc and free - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1147-171K
Language
English (United States)

If a UDF needs to allocate memory for its usage, you should use Teradata C library functions FNC_malloc and FNC_free instead of the standard C library functions malloc and free. The sqltypes_td.h header file redefines malloc and free to call FNC_malloc and FNC_free.

FNC_free and FNC_malloc check to make sure the UDF releases all memory before exiting and give an exception on the transaction if the UDF does not release all temporary memory it allocated. This prevents memory leaks in the database.

If you circumvent the logic to call malloc and free directly, there is a good chance that a memory leak could occur even if the UDF frees up memory correctly. The reason is that a UDF can abort while it is running if a user aborts the transaction, or if the transaction aborts because of some constraint violation that might occur on another node unbeknownst to the running UDF.

WHEN you … THEN …
develop, test, and debug a UDF standalone include the malloc.h header file and use the standard malloc and free C library functions.
use CREATE FUNCTION to submit the UDF source code to the server do not include the malloc.h header file and use the definitions of malloc and free from the sqltypes_td.h header file.

The definitions are used when submitting UDF source code to the server, but not when submitting UDF objects.