SatView: Pointer Perfect, Part 1 - Pointer casting
(Page 4 of 5 )
Pointers can take on different identities, depending on how you wish to use them. You might find older C APIs using void* as function parameters to allow you to implement your own types. An API could, for example, allow you to provide function pointers that construct, destruct, read and print strings. Since the API is only concerned with void* it won’t care whether you are using char, wchar_t, std::string, BSTR or whatever. This way the integration of string into your code base remains up to you, although lack of types makes it unsafe. This is an interesting point we are definitely going to explore later on.
In C++, the API could use pointers (preferably abstract) to base classes instead of void*. It does all its operations on the base class and leaves the implementation up to you in a derived class. Through virtual functions, the compiler will know which functions have to be called at runtime (the world of polymorphic classes). This way you can integrate your own code in a type safe way.
In order to use the different forms of a pointer, you sometimes need to cast them into their right shape. A nice rule to remember is that a cast doesn’t show respect for identity; so always verify how much respect your code is showing. Don’t be surprised to find bug-riddled code to be showing no respect at all.
C-style cast
The standard C-style cast is one that simply forces one type onto another.
T t = (T)expression;
e.g. float *pPtr2 = (float*)pMyVar;
C-style casts are totally unsafe (meaning they don’t perform neither compile time nor run time checks), so C++ provides operators that enhance these casts. Although the C++ cast operators require more work as far as typing is concerned, they will help you catch bugs early in the development process.
Static_cast
The static_cast works like the C-style cast and relies only on compile time information and performs no run time checks.
T t = static_cast<T>(expression);
e.g. float *pPtr2 = static_cast<float*>(myVar);
When you are dealing with a chunk of memory pointed at by char* (sizeof(char)==1... thus the pointer allows you to iterate byte for byte through the memory), there is an easy way to extract data types from this chunk. All you need is the reference operator and the static_cast:
int framerate = *(static_cast<int*>(&pChunk[n]));
What happens is that the address of the nth byte in pChunk is retrieved and casted from a char* to an int*, on which we use the reference operator. This forces the retrieval of the next 4 bytes (sizeof(int)==4), the contents of which are copied as an int into framerate. Always make sure that the memory you are reading really is available and that you are aware of any possible endianity issues. Endianity is something you deal with when you are writing cross-platform code and will be explained it in another article.
Next: Dynamic_cast >>
More Code Examples Articles
More By J. Nakamura