[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Inserting a struct in a buffer

Slain

11/3/2008 4:37:00 PM

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
2 Answers

Victor Bazarov

11/3/2008 5:12:00 PM

0

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

Salt_Peter

11/3/2008 10:43:00 PM

0

On Nov 3, 11:36 am, Slain <Slai...@gmail.com> 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?
>
> Thanks

I'm guessing you have some type of fixed-size buffer you are
collecting data in.
Passing an address is simple enough, the trouble is whether the
address means anything to the target receiving that packet.

Take a socket sending packets of data for example. The last thing you
want is meaningless data being transferred. Normally you would send
only valid data using some character count to delimit the extents of
the payload. So it makes sense to insert your struct instance in the
data portion.

Inserting the struct's address or the contents of the struct (with
padding removed) outside of what constitutes the packet's payload is
useless.

To compound the issue, a struct may well have padding in order to
align data.
so the size of an instance of Data is not neccesarily the sum of its
member's sizes.

#include <iostream>
#include <vector>

struct Data
{
char c;
int n;
double d;
};

int main()
{
std::cout << sizeof(char) << std::endl;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(double) << std::endl;

Data data;
std::cout << sizeof(data) << std::endl;
}

/*

1
4
8
16 // is not 13

*/

Thats where operator<< and operator>> enter into the design so as to
eliminate padding when inserting the data into a stream. Its pointless
to offer any more information since we have no idea how that packet is
being processed.