473,695 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getMyTok en();
MyId cId = record.getMyId( );
MyDate cDate = record.getMyDat e();
long cLength = record.getLengt h();

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.ge t()));
p += sizeof(cDate.ge t());
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 3738
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.getMyTok en();
MyId cId = record.getMyId( );
MyDate cDate = record.getMyDat e();
long cLength = record.getLengt h();

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.ge t()));
p += sizeof(cDate.ge t());
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.getMyTok en();
MyId cId = record.getMyId( );
MyDate cDate = record.getMyDat e();
long cLength = record.getLengt h();

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.ge t()));
p += sizeof(cDate.ge t());
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.commandToke n = record.getMyTok en();
Tmp.id = record.getMyId( );
Tmp.date = record.getMyDat e();
Tmp.length = record.getLengt h();

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.commandToke n;
... = 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(s truct 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.bu f[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
17505
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 library function? That is, can I be sure that every c++ standard library has this function? Or is there a c++ alternative to it?
6
7517
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 user-defined types. Now my confusion is here - a simple instantiation of the standard copy algorithm can be quite slow compared to the older memcpy for obvious reasons - specially for a large array of built-in types like int or something; so do...
16
15886
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 <string.h> void *memcpy(void *dest, const void *src, size_t n);
33
33716
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
14
4121
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 a function that would not clobber the parameter? Just surprised me a little is all.
6
2938
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 use '&' operator while using memcpy....i have code that use '&' and the code that call memcpy without '&' like is the following same Quest1 ---
39
19633
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) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
18
21569
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 memcpy to unload the data. Do I need to specify the source data as volatile in this case? What is the correct syntax for specifying that the data pointed to by the source pointer is volatile and not the pointer itself?
18
2698
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
8640
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8860
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7670
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4348
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.