overlay class over memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lromanus@ftml.net

    #1

    overlay class over memory

    I am relatively new to C++ so this is probably a basic question: Can I
    overlay a class over a chuck of memory just like one can overlay a
    struct over memory. I need to parse a frame comming from the network
    hardware.

    Thanks,

    Piotr

  • Victor Bazarov

    #2
    Re: overlay class over memory

    lromanus@ftml.n et wrote:[color=blue]
    > I am relatively new to C++ so this is probably a basic question: Can I
    > overlay a class over a chuck of memory just like one can overlay a
    > struct over memory. I need to parse a frame comming from the network
    > hardware.[/color]

    You should take a look at "placement new".

    V
    --
    Please remove capital As from my address when replying by mail


    Comment

    • Frank Schmidt

      #3
      Re: overlay class over memory


      <lromanus@ftml. net> schrieb im Newsbeitrag
      news:1140920987 .611858.215690@ u72g2000cwu.goo glegroups.com.. .[color=blue]
      >I am relatively new to C++ so this is probably a basic question: Can I
      > overlay a class over a chuck of memory just like one can overlay a
      > struct over memory. I need to parse a frame comming from the network
      > hardware.[/color]

      Only if your class looks like a classic struct. If you add "hidden"
      varialbles to the class, e.g. a a pointer to the virtual function table do
      to a virtual function in the class, ... it will mess up the memory
      alignment.


      Comment

      • lromanus@ftml.net

        #4
        Re: overlay class over memory

        Is placement new fixing the problem of virtual tables?

        Piotr

        Comment

        • Victor Bazarov

          #5
          Re: overlay class over memory

          lromanus@ftml.n et wrote:[color=blue]
          > Is placement new fixing the problem of virtual tables?[/color]

          What book are you reading that doesn't explain 'new'? "Placement
          new" is not different at all in the procedure of constructing the
          object. The only difference is actual memory allocation, the
          "regular" 'new' allocates, the "placement" doesn't.

          V
          --
          Please remove capital As from my address when replying by mail


          Comment

          • Gianni Mariani

            #6
            Re: overlay class over memory

            lromanus@ftml.n et wrote:[color=blue]
            > I am relatively new to C++ so this is probably a basic question: Can I
            > overlay a class over a chuck of memory just like one can overlay a
            > struct over memory. I need to parse a frame comming from the network
            > hardware.[/color]

            Simply casting the memory *might* work except for alignment and byte order.

            This is a posting I did a while ago - might help.


            Comment

            Working...