SQLite Forum

Freeing memory used in sqlite£-result_blob
Login
The most memory efficient and fastest way would be example 2.

Assuming you are calling the user defined function twice, the memory subsystem calling sequence would be:

malloc #1 (by fct)
malloc #1 (by SQLite)
memcpy #1 (by SQLite)
free   #1 (by fct)
malloc #2 (by fct - possibly the same address as before)
malloc #2 (by SQLite)
memcpy #2 (by SQLite)
free   #2 (by fct)
...
free   #1 (by SQLite)
free   #2 (by SQLite)

total 4x malloc, 4x free, 2x memcpy, max 3 blobs concurrently allocated

versus

malloc #1 (by fct)
malloc #2 (by fct)
...
free   #1 (by SQLite)
free   #2 (by SQLite)

total 2x malloc, 2x free, max 2 blobs concurrently allocated

versus

(allocate on stack in fct)
malloc #1 (by SQLite)
memcpy #1 (by SQLite)
(allocate on stack in fct)
malloc #2 (by SQLite)
memcpy #2 (by SQLite)
...
free   #1 (by SQLite)
free   #2 (by SQLite)

total 2x malloc, 2x memcpy, max 2 blobs concurrently allocated