dbTalk Databases Forums  

How long live the DBT.data filed?

comp.databases.berkeley-db comp.databases.berkeley-db


Discuss How long live the DBT.data filed? in the comp.databases.berkeley-db forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
NightThunder
 
Posts: n/a

Default How long live the DBT.data filed? - 08-24-2006 , 06:57 PM






Hi, all.

I want to know the scope od datafield of DBT struct.

When i get one record, the data struct has valid data region pointer.
but how can I assure a validity of that pointer?

when I return that pointer, is valid that data region in memory?

Thanks in advance.


Reply With Quote
  #2  
Old   
NightThunder
 
Posts: n/a

Default Some Example code snipet. - 08-28-2006 , 12:12 AM






BYTE* ReturnAsPointer()
{
DBT key, data;
....................
return data.data;

}
void TestFunc()
{
BYTE * pReturnData = ReturnAsPointer();

}


In TestFunc, is the pReturnData valid?
if not valid, should i must allocating heap memory and copy to there?



NightThunder 작성:

Quote:
Hi, all.

I want to know the scope od datafield of DBT struct.

When i get one record, the data struct has valid data region pointer.
but how can I assure a validity of that pointer?

when I return that pointer, is valid that data region in memory?

Thanks in advance.


Reply With Quote
  #3  
Old   
Andrei Costache
 
Posts: n/a

Default Re: Some Example code snipet. - 08-31-2006 , 06:28 AM



Hi NightThunder,

Please note that all fields of the DBT structure that are not
explicitly set should be initialized to null bytes before the first
time the structure is used.
In your case, the code would look like this in the ReturnAsPointer
function:

BYTE* ReturnAsPointer()
{
DBT key,data;
char* desc = "Content of the data field for the data DBT";
.................
/* Set to 0 the fields of the DBTs */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
.................
data.data = desc;
data.size = (u_int32_t) strlen(desc) + 1;
................
return (BYTE*) data.data;
}

In the TestFunc function the pReturnData pointer can't be used to
modify the content of the data.data location. But still you can use it,
let's say to display the content of data.data. In order to use a valid
pointer you should allocate heap memory like:

BYTE* pReturnData = ReturnAsPointer();
u_int32_t strSize = (u_int32_t) strlen(pReturnData) + 1;

BYTE* pTestData = (BYTE*) malloc(strSize);

A test function for you could be:

void TestFunc()
{
BYTE* pReturnData = ReturnAsPointer();
u_int32_t strSize = (u_int32_t) strlen(pReturnData) + 1;

BYTE* pTestData = (BYTE*) malloc(strSize);

cout<<"\n Data field of the data DBT is: "<<pReturnData;
cout<<"\n Size of data field of the data DBT is: "<<strSize<<"\n";
..............................
}

Regards,

Andrei Costache
Berkeley DB
Oracle Support Services


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.