Inserting a struct in a buffer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Slain

    Inserting a struct in a buffer

    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
  • Victor Bazarov

    #2
    Re: Inserting a struct in a buffer

    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

    Comment

    Working...