placement new and bit fields.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ma740988@pegasus.cc.ucf.edu

    placement new and bit fields.


    If I understand placement new. Object destruction requires an explicit
    call to the destructor.

    struct my_struct {
    unsigned int val1 : 5;
    unsigned int val2 : 4;
    unsigned int reserved : 23;
    };

    Now given an address - say 0x40000000. For the case where I have bit
    fields. What's the approach on the constructor/destructor here?

    I suspect:

    my_struct *m = new (0x40000000) my_struct;
    // clean up?

    Thanks in advance

  • Srini

    #2
    Re: placement new and bit fields.

    > If I understand placement new. Object destruction requires an explicit[color=blue]
    > call to the destructor.
    >
    > struct my_struct {
    > unsigned int val1 : 5;
    > unsigned int val2 : 4;
    > unsigned int reserved : 23;
    > };
    >
    > Now given an address - say 0x40000000. For the case where I have bit
    > fields. What's the approach on the constructor/destructor here?
    >
    > I suspect:
    >
    > my_struct *m = new (0x40000000) my_struct;
    > // clean up?
    >
    > Thanks in advance[/color]

    Are you asking how to explicitly call the destructor. Its this way...

    m->my_struct::~my _struct();

    What approach for ctor/dtor?

    There are 2 things here. When you say,
    [color=blue]
    > my_struct *m = new (0x40000000) my_struct;[/color]

    Where did you get that address from? If that was obtained by dynamic
    allocation, you'll have to free it yourself. ctor and dtor are only for
    allocation, initialization and de-allocation, respectively of the
    *members* of the struct. In your case, there's no dynamic allocation of
    the members in your struct. So there's nothing that you need to do in
    the destructor.

    So, in essence, your code might look something like this...

    void * ptr = malloc(some_siz e);
    // lets assume that ptr has the value you'd specified (0x40000000)
    my_struct *m = new (ptr) my_struct;
    // ... use the object
    m->my_struct::~my _struct(); // explicit dtor call
    free(ptr); // free up the originally allocated memory

    Oh btw - I assume that you've provided a placement new i your struct.

    HTH
    Srini

    Comment

    • ma740988@pegasus.cc.ucf.edu

      #3
      Re: placement new and bit fields.

      || m->my_struct::~my _struct();

      For starters, I suspect it's leagal to put destructors in bit fields...
      so now:

      struct my_struct {
      unsigned int val1 : 5;
      unsigned int val2 : 4;
      unsigned int reserved : 23;
      ~my_struct();
      };

      Thats ok?

      Now... The address I'll use is obtained from a vendor function. So
      now:

      unsigned int addr0;
      STATUS stat;
      // later
      stat = sysPciConfigRea d(PCI_CFG_BASE_ ADDRESS_0, 4, &addr0);

      Now addr0 is 'valid'. The contents at addr0 is really a a register
      definition that looks like my_struct above..
      To simpify things .. I thought well this is a candidate for
      placement new:

      my_struct *m = new ((void*)addr0) my_struct;

      In a case like this, my guess is I really dont need to 'worry' abotu
      'freeing' anything per se... so I might be ok..

      Comment

      • Srini

        #4
        Re: placement new and bit fields.

        > For starters, I suspect it's leagal to put destructors in bit fields...[color=blue]
        >[/color]

        I don't quite understand the phrase above. Every struct/class in C++
        *must* have a destructor. If you don't provide one, the compiler will.
        You have to provide a destructor in case you need to clean up the
        members of the object being destroyed. If you don't clean up it will
        cause a memory leak. In your case, there are no members that are being
        dynamically allocated and hence don't need any clean-up to be done in
        the destructor. So, the compiler provided destructor will suffice.
        [color=blue]
        >
        > Now... The address I'll use is obtained from a vendor function. So
        > now:
        >
        > unsigned int addr0;
        > STATUS stat;
        > // later
        > stat = sysPciConfigRea d(PCI_CFG_BASE_ ADDRESS_0, 4, &addr0);
        >
        > Now addr0 is 'valid'. The contents at addr0 is really a a register
        > definition that looks like my_struct above..
        >[/color]

        I cannot comment much on this since I have no knowledge of the above
        vendor library function. But since you've told that the contents at the
        obtained address is a register with fields as in your struct, let me
        warn you of something. The code might not be portable. Because the
        allocation of bits within a byte are unspecified in the standard - This
        means the compilers can allocate bits starting from either MSB or LSB.
        Therefore it might be more prudent to use appropriate masks and bitwise
        operators to extract values from that address.

        Srini

        Comment

        Working...