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

SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Published
July 2021
Language
English (United States)
Last Update
2021-07-27
dita:mapPath
rin1593638965306.ditamap
dita:ditavalPath
rin1593638965306.ditaval
dita:id
B035-1147
lifecycle
previous
Product Category
Teradata Vantage™

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.