casting from raw bytes to structures.

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

    casting from raw bytes to structures.

    In an effort to get up to date with C to C++, I've ran into a problem.

    I am using an 3rd-party library that returns the raw amount of bytes ot
    allocate for a structure that it also uses. So I do something as
    follows:

    FeaturesStruct* pFeatures = (FeaturesStruct *) new BYTE[StructSize];

    This works fine with the API for all intents and purposes.

    However, when changing the C style cast to a static_cast, I cannot
    compile it.
    Using VC++ I get:

    Error 1 error C2440: 'static_cast' : cannot convert from 'BYTE *' to
    'FeaturesStruct *'

    I was curious as to what the correct way to allocate the structure
    would be in this case would be, or if I'm just stuck using C style
    casts because of the way the library functions?

    Thanks,
    Josh McFarlane

  • Tobias Blomkvist

    #2
    Re: casting from raw bytes to structures.

    Josh Mcfarlane sade:[color=blue]
    > In an effort to get up to date with C to C++, I've ran into a problem.
    >
    > I am using an 3rd-party library that returns the raw amount of bytes ot
    > allocate for a structure that it also uses. So I do something as
    > follows:
    >
    > FeaturesStruct* pFeatures = (FeaturesStruct *) new BYTE[StructSize];
    >[/color]
    [snip][color=blue]
    >
    > Error 1 error C2440: 'static_cast' : cannot convert from 'BYTE *' to
    >[/color]

    FeaturesStruct * pFeatures = reinterpret_cas t<FeaturesStruc t*>(new
    BYTE[StructSize]);

    Tobias
    --
    IMPORTANT: The contents of this email and attachments are confidential
    and may be subject to legal privilege and/or protected by copyright.
    Copying or communicating any part of it to others is prohibited and may
    be unlawful.

    Comment

    • Victor Bazarov

      #3
      Re: casting from raw bytes to structures.

      Josh Mcfarlane wrote:[color=blue]
      > In an effort to get up to date with C to C++, I've ran into a problem.
      >
      > I am using an 3rd-party library that returns the raw amount of bytes ot
      > allocate for a structure that it also uses. So I do something as
      > follows:
      >
      > FeaturesStruct* pFeatures = (FeaturesStruct *) new BYTE[StructSize];
      >
      > This works fine with the API for all intents and purposes.
      >
      > However, when changing the C style cast to a static_cast, I cannot
      > compile it.
      > Using VC++ I get:
      >
      > Error 1 error C2440: 'static_cast' : cannot convert from 'BYTE *' to
      > 'FeaturesStruct *'
      >
      > I was curious as to what the correct way to allocate the structure
      > would be in this case would be, or if I'm just stuck using C style
      > casts because of the way the library functions?[/color]


      Use "placement new":

      BYTE* space = new BYTE[sizeof(Features Struct)];
      FeaturesStruct* pFeatures = new (space) FeaturesStruct;

      Don't forget to destruct the object when you don't need it any more:

      pFeatures->~FeaturesStruc t();

      and free the memory

      delete[] space;

      V

      Comment

      • Old Wolf

        #4
        Re: casting from raw bytes to structures.

        Tobias Blomkvist wrote:[color=blue]
        > Josh Mcfarlane sade:[color=green]
        > > In an effort to get up to date with C to C++, I've ran into a problem.[/color][/color]

        Do you mean you are porting a C program to C++ ? If so , then
        it's fine to leave 'malloc' and 'free' calls as they are.
        (Perhaps adding in casts where necessary).
        [color=blue][color=green]
        >>
        >> I am using an 3rd-party library that returns the raw amount of bytes ot
        >> allocate for a structure that it also uses. So I do something as
        >> follows:
        >>
        >> Error 1 error C2440: 'static_cast' : cannot convert from 'BYTE * to[/color]
        >
        > FeaturesStruct * pFeatures = reinterpret_cas t<FeaturesStruc t*>(new
        > BYTE[StructSize]);[/color]

        Remember that when you delete pFeatures, you must delete in the same
        way that it was allocated:

        delete [] reinterpret_cas t<BYTE *>(pFeatures);

        My preferred solution (assuming FeaturesStruct is a POD and
        doesn't require destruction and construction) is:

        std::vector<BYT E> features_buf (StructSize);
        FeaturesStruct *pFeatures = &features_bu f[0];

        then you no longer have to worry about memory management.

        Comment

        • msalters

          #5
          Re: casting from raw bytes to structures.


          Josh Mcfarlane schreef:
          [color=blue]
          > In an effort to get up to date with C to C++, I've ran into a problem.
          >
          > I am using an 3rd-party library that returns the raw amount of bytes ot
          > allocate for a structure that it also uses. So I do something as
          > follows:
          >
          > FeaturesStruct* pFeatures = (FeaturesStruct *) new BYTE[StructSize];
          >
          > This works fine with the API for all intents and purposes.
          >
          > However, when changing the C style cast to a static_cast, I cannot
          > compile it.
          > Using VC++ I get:
          >
          > Error 1 error C2440: 'static_cast' : cannot convert from 'BYTE *' to
          > 'FeaturesStruct *'
          >
          > I was curious as to what the correct way to allocate the structure
          > would be in this case would be, or if I'm just stuck using C style
          > casts because of the way the library functions?[/color]

          Who creates the object? You, or the library? In your code pFeatures
          points to uninitialized memory suitable for allocation of a
          FeaturesStruct
          object.

          If you use placement new:
          FeaturesStruct* pFeatures = new ( new char[StructSize] )
          FeaturesStruct;
          you not ony get the memory needed, you get a constructed FeaturesStruct
          object as well.

          HTH,
          Michiel Salters

          Comment

          Working...