Skip to Main Content

Berkeley DB Family

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get rid of extra data in character buffers for key and data

609342Nov 19 2007 — edited Nov 20 2007
Hi -

Using the C API, I am using a cursor to just pull all the key/value pairs from a database.
Here is the relevant code:

/* Initialize key/data structures. */
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));

key.flags = DB_DBT_MALLOC;
data.flags = DB_DBT_MALLOC;

if((ret = dbp->cursor(dbp, NULL, &cursor, 0)) != 0) {
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}

while((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
fprintf(cgiOut, "%s|||%s\n", (char *)key.data, (char *)data.data);
free(key.data);
free(data.data);
}
if(ret != DB_NOTFOUND) {
dbp->err(dbp, ret, "cursor->c_get");
goto err;
}


This produces lines that in many cases contain extra characters at the ends, as though the malloced data was not being cleared before use. I wrote this routine originally in Java, and solved the problem there with the "setReuseBuffer(false)" Database Entry routine. Is there something similar in C?

Garey Mills
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 18 2007
Added on Nov 19 2007
3 comments
928 views