By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,946 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,946 IT Pros & Developers. It's quick & easy.

Inserting a struct in a buffer

P: n/a
I have a buffer, which has a header and then data. There is some
leeway space between the header and the data in the buffer.

I have to add some more data which is a struct and I know the size of
it. So if I get a pointer to the beginning of the start of data, go
back the size of my struct, can I just put the address of my struct ?

Packet A -- Header + Data
Struct B --size X bytes

Pointer_to_Data
Pointer_to_Data = Pointer_to_Data - X

Is it recommended to copy the struct or can I just pass the address of
my sturct variable. If so how?

Thanks
Nov 3 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Slain wrote:
I have a buffer, which has a header and then data. There is some
leeway space between the header and the data in the buffer.

I have to add some more data which is a struct and I know the size of
it. So if I get a pointer to the beginning of the start of data, go
back the size of my struct, can I just put the address of my struct ?

Packet A -- Header + Data
Struct B --size X bytes

Pointer_to_Data
Pointer_to_Data = Pointer_to_Data - X

Is it recommended to copy the struct or can I just pass the address of
my sturct variable. If so how?
I am not sure what you're asking. Do you want to know how to take the
address of your struct? Use the '&' operator. Everything else if
really up to you. It is completely up to you whether to put the address
or the struct itself into your buffer. Does the struct live long enough
at the same address so that the pointer does not go invalid? If so, you
could use the address. Is it a short-lived struct, but the information
is valid for a long time, store the [copy of the] information itself.
You didn't provide enough detail to make the determination about that
design. And, you will have to forgive me, but I don't see any C++
language question in here, really. You seem to need to manipulate some
low-level values, like addresses in memory, which isn't really how C++
should be used...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.