I have several questions about DBT flag, who could help clarify them?
I am clear about flag DB_DBT_MALLOC/DB_DBT_USERMEM, but when I use get without cursors, what the memory to which the pointer stored into the DBT refers if I use flag 0? Do I need to free the memory?
If use cursor get, I see examples in the manual seem always use flag 0. What the memory refers? Is it safe to change the data it refers then put the new data? Do I need to free the memory? Can I use DB_DBT_MALLOC/DB_DBT_USERMEM when use cursor get? Is it ok to use cursor get one record first, then use put to replace this record's data without cursor?
Can you give suggestion when to use flag 0?
Thanks in advance!
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?