By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,122 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,122 IT Pros & Developers. It's quick & easy.

casting from raw bytes to structures.

P: n/a
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

Aug 4 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
--
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.
Aug 4 '05 #2

P: n/a
Josh Mcfarlane wrote:
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
Aug 4 '05 #3

P: n/a
Tobias Blomkvist wrote:
Josh Mcfarlane sade:
In an effort to get up to date with C to C++, I've ran into a problem.
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.

Aug 4 '05 #4

P: n/a

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

Aug 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.