473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

memcpy confusion

Ok, it's been a while since I've done the whole memcpy stuff with C++
and I'm having a hard time remembering everything.

I hope, however, that you can help me with my problem.

I memcpy a struct into a buffer to put it into a database (BerkeleyDB,
to be specific) - what do I do to memcpy the thing back?

Code Example:
//struct I want to work with
struct dbHeader {
MyToken commandToken;
MyId id;
MyDate date;
unsigned long length;
};

where MyToken is an enum and MyId as well as MyDate are classes with a
private member unsigned char value[14]; and just set/get methods ( eg
inline void set( const unsigned char *s ) { strncpy( (char *)value,
(char *)s, 14); }; ) this should be correct so far.

now I want to memcpy the memory into a buffer to store into the
database.

MyToken cCommand = record.getMyToken();
MyId cId = record.getMyId();
MyDate cDate = record.getMyDate();
long cLength = record.getLength();

size_t len;
u_int8_t *p, data_buffer[1024];
p = &data_buffer[0];
len = sizeof(cCommand);
memcpy(p, &len, sizeof(len));
p += sizeof(len);
memcpy(p, &cCommand, len);
p += len;
memcpy(p, cId.get(), sizeof(cId.get()));
p += sizeof(cId.get());
memcpy(p, cDate.get(), sizeof(cDate.get()));
p += sizeof(cDate.get());
memcpy(p, &cLength, sizeof(cLength));
p += sizeof(cLength);

(cId.get() returns a char*)

Ok, p has the struct now, has it?

Now I put it into the db and when I want it back, how do I read it back
to my struct?

I hope you can help me.

Sep 20 '05 #1
5 3701
manya wrote:
Ok, it's been a while since I've done the whole memcpy stuff with C++
and I'm having a hard time remembering everything.

I hope, however, that you can help me with my problem.

I memcpy a struct into a buffer to put it into a database (BerkeleyDB,
to be specific) - what do I do to memcpy the thing back?

Code Example:
//struct I want to work with
struct dbHeader {
MyToken commandToken;
MyId id;
MyDate date;
unsigned long length;
};

where MyToken is an enum and MyId as well as MyDate are classes with a
private member unsigned char value[14]; and just set/get methods ( eg
inline void set( const unsigned char *s ) { strncpy( (char *)value,
(char *)s, 14); }; ) this should be correct so far.

now I want to memcpy the memory into a buffer to store into the
database.

MyToken cCommand = record.getMyToken();
MyId cId = record.getMyId();
MyDate cDate = record.getMyDate();
long cLength = record.getLength();

size_t len;
u_int8_t *p, data_buffer[1024];
p = &data_buffer[0];
len = sizeof(cCommand);
memcpy(p, &len, sizeof(len));
p += sizeof(len);
memcpy(p, &cCommand, len);
p += len;
memcpy(p, cId.get(), sizeof(cId.get()));
p += sizeof(cId.get());
memcpy(p, cDate.get(), sizeof(cDate.get()));
p += sizeof(cDate.get());
memcpy(p, &cLength, sizeof(cLength));
p += sizeof(cLength);

(cId.get() returns a char*)

Ok, p has the struct now, has it?

Now I put it into the db and when I want it back, how do I read it back
to my struct?

I hope you can help me.


This is a good approach using C idioms. However, you should consider
the C++ techniques described in these FAQs:

http://www.parashift.com/c++-faq-lit...alization.html

The Boost libraries have a serialization library that may be of some
assistance also:

http://boost.org/libs/serialization/doc/index.html

The serialization approach allows each class to serialize and
unserialize itself. Your approach makes extra copies (though they could
potentially be eliminated) and tightly couples the implementation of
the record object to the code that does the serialization. Tight
coupling leads to programs that are harder to understand and maintain.

Cheers! --M

Sep 20 '05 #2
manya wrote:

now I want to memcpy the memory into a buffer to store into the
database.

MyToken cCommand = record.getMyToken();
MyId cId = record.getMyId();
MyDate cDate = record.getMyDate();
long cLength = record.getLength();

size_t len;
u_int8_t *p, data_buffer[1024];
p = &data_buffer[0];
len = sizeof(cCommand);
memcpy(p, &len, sizeof(len));
p += sizeof(len);
memcpy(p, &cCommand, len);
p += len;
memcpy(p, cId.get(), sizeof(cId.get()));
p += sizeof(cId.get());
memcpy(p, cDate.get(), sizeof(cDate.get()));
p += sizeof(cDate.get());
memcpy(p, &cLength, sizeof(cLength));
p += sizeof(cLength);

(cId.get() returns a char*)

Ok, p has the struct now, has it?
Yes, it has. You successfully eliminated possible struct padding in doing so.
If padding is not an issue, you simply could have done:

dbHeader Tmp;
Tmp.commandToken = record.getMyToken();
Tmp.id = record.getMyId();
Tmp.date = record.getMyDate();
Tmp.length = record.getLength();

u_int8_t data_buffer[1024]; // don't know if 1024 is vital, dimensioning the
// array with sizeof(dbHeader) usually does the trick

memcpy( data_buffer, &Tmp, sizeof( Tmp ) ); // In C++ some casts will be needed

Now I put it into the db and when I want it back, how do I read it back
to my struct?


Reverse everything. That is: reverse source with destination
For my example above (the short version would be)

// come up with the bytes in data_buffer, read it from the DB

dbHeader Tmp;
memcpy( &Tmp, data_buffer, sizeof( Tmp ) ); // again, some casts will be needed
// to feed memcpy with void pointers

... = Tmp.commandToken;
... = Tmp.id;
...

or in your long version

u_int8_t *p;
p = &data_buffer[0];

size_t len;
memcpy( &len, p, sizeof( len ) );

p += sizeof(len);
memcpy( &cCommand, p, len ); // you should assert that sizeof( MyToken ) == len here

....

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 20 '05 #3
thanks, I followed the instructions at Berkeley
(http://www.sleepycat.com/docs/ref/am_misc/struct.html) that are
written in C Style and haven't even thought about Serialization (tah,
once again I should think about thinking outside of the box ;-) )

Thanks so much for the tip and the links! I'll try it out tomorrow -
hopefully it'll work together with what I did with Berkeley.

Sep 20 '05 #4
thanks, with trial and error I found that out just after I posted it.

Sep 20 '05 #5
manya wrote:
thanks, I followed the instructions at Berkeley
(http://www.sleepycat.com/docs/ref/am_misc/struct.html) that are
written in C Style and haven't even thought about Serialization (tah,
once again I should think about thinking outside of the box ;-) )

Thanks so much for the tip and the links! I'll try it out tomorrow -
hopefully it'll work together with what I did with Berkeley.


If you're working in C++, I'd suggest going the serialization route. If
you're working in C (even if you're using a C++ compiler), go ahead and
use their techniques. Mixing C-style code with C++ can sometimes lead
to problems, so I avoid doing it when possible.

BTW, I looked at that link. Their second method is filthy, disgusting,
and just plain vile (anagram of evil) in the C++ world:

------
The second way to solve this problem only works if you have just one
variable length field in the structure. In that case, you can declare
the structure as follows:

struct {
int a, b, c;
u_int8_t buf[1];
} info;

Then, let's say you have a string you want to store in this structure.
When you allocate the structure, you allocate it as:

malloc(sizeof(struct info) + strlen(string));

Since the allocated memory is contiguous, you can the initialize the
structure as:

info.a = 1;
info.b = 2;
info.c = 3;
memcpy(&info.buf[0], string, strlen(string) + 1);

and give it to Berkeley DB to store, with a length of:

sizeof(struct info) + strlen(string);

In this case, the structure can be copied out of the database and used
without any additional work.

Sep 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
6
by: Samee Zahur | last post by:
Hi all, I'm a little confused - my guess is memcpy is no longer (or perhaps never was) a standard c++ function, since it has very little type check into it - and can potentially create havoc for...
16
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the following prototype of memcpy(3): #include...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
14
by: Michael B Allen | last post by:
I just noticed that doing something like the following may fail because it can overwrite u->size before it's evaluated. memcpy(u, buf, u->size); Is it legit that this is a macro as opposed to...
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
18
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.