scoped_array and legacy APIs
Hey-hey, Is it proper to to use static_cast like this: //Library.h - legacy library typedef ??? Type; //library-defined, we don't know the actual type typedef Type* PType; //library-defined pointer type //sets bufSize to # bytes necessary if buf not big enough LibraryFunction(PType buf, int* bufSize); //MyCode.cpp int bytesNeeded = 0; LibraryFunction(0, &bytesNeeded); scoped_array<Type>(static_cast<PType>(new BYTE[bytesNeeded])); The legacy library tells us storage requirements in BYTEs, but I want to deal with 'Type's, not BYTEs. Is there a conceivable reason why this could not be safe, even if it happens to be with the current implementation? Thanks. - Sean
So long as Type has no ctor/dtor, I don't see a reason why it wouldn't work.
On 4/20/05, Sean DeNigris
Hey-hey,
Is it proper to to use static_cast like this:
//Library.h - legacy library
typedef ??? Type; //library-defined, we don't know the actual type
typedef Type* PType; //library-defined pointer type
//sets bufSize to # bytes necessary if buf not big enough
LibraryFunction(PType buf, int* bufSize);
//MyCode.cpp
int bytesNeeded = 0;
LibraryFunction(0, &bytesNeeded);
scoped_array<Type>(static_cast<PType>(new BYTE[bytesNeeded]));
The legacy library tells us storage requirements in BYTEs, but I want to deal with 'Type's, not BYTEs.
Is there a conceivable reason why this could not be safe, even if it happens to be with the current implementation?
Thanks.
- Sean _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Cory Nelson http://www.int64.org
Sean DeNigris wrote:
Hey-hey,
Is it proper to to use static_cast like this:
//Library.h - legacy library typedef ??? Type; //library-defined, we don’t know the actual type typedef Type* PType; //library-defined pointer type
//sets bufSize to # bytes necessary if buf not big enough LibraryFunction(PType buf, int* bufSize);
//MyCode.cpp int bytesNeeded = 0; LibraryFunction(0, &bytesNeeded); scoped_array<Type>(static_cast<PType>(new BYTE[bytesNeeded]));
The legacy library tells us storage requirements in BYTEs, but I want to deal with ‘Type’s, not BYTEs.
Is there a conceivable reason why this could not be safe, even if it happens to be with the current implementation?
Arrays on the heap must be deleted using the same pointer type and value yielded by new. Deletion using the result of any type conversion has undefined behaviour. Also, unless Type is declared as void the static_cast is ill-formed. You're going to need to use a separate scoped_array<BYTE> and pointer to Type. Ben.
On 4/20/05, Sean DeNigris
LibraryFunction(0, &bytesNeeded); scoped_array<Type>(static_cast<PType>(new BYTE[bytesNeeded]));
I'm not sure if that would nessesarily work properly depending on how your architecture requires things to be aligned... How about this?: scoped_array<Type>( new Type[ bytesNeeded / sizeof(Type) ] ); I think that should work, so long as LibraryFunction includes any padding. - Scott McMurray
participants (4)
-
Ben Hutchings
-
Cory Nelson
-
me22
-
Sean DeNigris