When you use 0, BerkeleyDB will allocates memory for you, and when you close the cursor, the memory will be freed, so there is no need for you to free the memory. But the content of this chunk of memory will be overwritten when you call next cursor get, so you need to backup the content if you need it after another cursor get. You can change the data, but should not change the bytes out of this chunk, so we suggest using your own memory to composing and putting the data instead of changing the returned data directly.
About using DB_DBT_MALLOC/DB_DBT_USERMEM for cursor get, please check http://docs.oracle.com/cd/E17076_03/html/api_reference/C/dbt.html for the detailed information.
Finally, if your database is not set with DB_DUPSORT or DB_DUP, you can overwrite a record without cursor. Otherwise, you need to use cursor to overwrite data in most cases. Here, the overwriting means to change the data, not the key. If you need to change key as well, you need to do an operation sequence of delete-add.
For more information about cursor get and cursor put, please check the DBcursor->get and DBcursor->put at Chapter 3. The DBcursor Handle
Thank you for the very detailed answer. Now I am clear with cursor get, with flag 0 memory will be overwritten when call next cursor get and freed when call cursor close. Just one more question: with flag 0, call db->get (not cursor get), when will the memory allocated by db be overwritten and freed?
1 person found this helpful
When using DB->get, the memory will be allocated and bundled with the DB handle. It will be overwritten by next DB->get call, and be freed when you close the DB handle.
-Winter, Oracle Berkeley DB
Got it. Thank you!