SQLite Forum

How do I use sqlite3_malloc in Sqlite
Login

How do I use sqlite3_malloc in Sqlite

(1) By simmo (simmo134) on 2021-03-19 20:07:29 [link] [source]

Good day, how do I use the following: SQLite uses the malloc(), realloc(), and free()

I have read the below but do not know how to incorporate it with an actual SQl statement. Say for example, I would like to use malloc with inserting data, how would do this? I have not seen any example.

https://www.sqlite.org/malloc.html sqlite3_malloc(N) etc

thanks for your time and consideration.

(2) By Larry Brasfield (larrybr) on 2021-03-19 20:34:25 in reply to 1 [link] [source]

You do not use those functions from SQL. You might use them within C code that uses the SQLite library by linking to it. You may want to consult some C tutorials.

(3) By anonymous on 2021-03-19 20:38:27 in reply to 2 [source]

Thats what I meant to use it in c++ code. Could you put me to any reference ?

On my mobile device Thanks

(4) By Larry Brasfield (larrybr) on 2021-03-19 21:16:28 in reply to 3 [link] [source]

When you wrote: "do not know how to incorporate [allocation functions] with an actual SQl statement" and "would like to use malloc with inserting data", I inferred that you do not yet know what the functions are for.

I suggest you do a web search for "malloc and free". This will turn up a plethora of sites where you can learn all about their purposes and usages.

(5) By anonymous on 2021-03-19 22:14:19 in reply to 4 [link] [source]

ok thanks. I cannot find any examples of using this "sqlite3_malloc()".

(6) By Larry Brasfield (larrybr) on 2021-03-19 22:34:47 in reply to 5 [link] [source]

If you consult the docs for the allocation functions, you will find that they operate just like the malloc() and free() your web search finds. And, as you can see from the SQLite docs and the thousands of examples your search will reveal, they operate the same way as the standard C library allocation functions. Hence, you can use those examples as examples of using sqlite3_malloc() and sqlite3_free(), (with a slight name change, of course.)

Here would be one, semi-useless example: sqlite3_free( sqlite3_malloc( 1234 ) ); . You may devise more useful examples once you decide what it is that you are trying to do beyond just use the functions.

(7) By Keith Medcalf (kmedcalf) on 2021-03-19 22:56:57 in reply to 5 [link] [source]

sqlite3_malloc / sqlite3_malloc64 / sqlite3_msize / sqlite3_realloc / sqlite3_realloc64 / sqlite3_free work exactly the same as the standard C functions by the same name but without the sqlite3_ prefix.

The difference is that the sqlite3_ prefixed versions use whatever memory allocation method is in use by the sqlite3 library which may be identical to the standard malloc/free subsystem using either your application or a separate arena (depending on how you linked your application and sqlite3 and your Operating System) or may use an entirely different heap and allocation method from your application entirely.

Basically the standard malloc/free allocate/release your applications memory your applications way and your application owns the memory. sqlite3_malloc/sqlite3_free allocate/release sqlite3's memory sqlite3's way and sqlite3 owns the memory, not your application.

(8) By anonymous on 2021-03-20 04:21:49 in reply to 3 [link] [source]

Here is an example how to sqlite_mallo:

/** brief Initialize the flag function extension * * param db a pointer to the database * param a point to a point for the error messages * param a pointer to the SQLITE_API * return SQLITE_OK * / int sqlite3_extension_init(sqlite3 db, char **err, const sqlite3_api_routines *api) { int rc = SQLITE_OK; int RegisterFlagFunctions(sqlite3 *);

SQLITE_EXTENSION_INIT2(api)
/**< Registers all flag functions */
rc = RegisterFlagFunctions(db);
if (rc != SQLITE_OK) {
    /* Error! */
    return rc;
}

/**< Need so memory from SQLite to store the flags */
sptr = sqlite3_malloc(sizeof(Semaphore));
ClearFlags();

return rc;

}

By the way the extension is not in control of the memory block. SQLite is in control and therefor I am not deleting anything. You should check sptr not being NULL.