casting from raw bytes to structures.

J

Josh Mcfarlane

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
 
T

Tobias Blomkvist

Josh Mcfarlane sade:
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];
[snip]

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

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

Tobias
 
V

Victor Bazarov

Josh said:
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?


Use "placement new":

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

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

pFeatures->~FeaturesStruct();

and free the memory

delete[] space;

V
 
O

Old Wolf

Tobias said:
Josh Mcfarlane sade:

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).
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

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

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

delete [] reinterpret_cast<BYTE *>(pFeatures);

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

std::vector<BYTE> features_buf (StructSize);
FeaturesStruct *pFeatures = &features_buf[0];

then you no longer have to worry about memory management.
 
M

msalters

Josh Mcfarlane schreef:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top