reinterpret_cast question
-
I am a novice at programming and am working my way through Feng Yuan's book "Windows Graphics Programming". It's great stuff and he includes extensive development of useful graphics classes that can be used in your programs. Anyway, I'm curious as to why he doesn't use the reinterpret_cast operator in his code when converting pointers. Typically he does something like this:
// query GDI for image size GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, NULL, (BITMAPINFO *) & dibinfo, DIB_RGB_COLORS); int nInfoSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * GetDIBColorCount(dibinfo.bmiHeader); int nTotalSize = nInfoSize + GetDIBPixelSize(dibinfo.bmiHeader); BYTE * pDIB = new BYTE[nTotalSize]; if ( pDIB ) { memcpy(pDIB, & dibinfo, nInfoSize); if ( ddbinfo.bmHeight != GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, pDIB + nInfoSize, (BITMAPINFO *) pDIB, DIB_RGB_COLORS) ) { delete [] pDIB; pDIB = NULL; } } ReleaseDC(NULL, hDC); return (BITMAPINFO *) pDIB;
This is a segment from one of his functions that converts a Device Dependent Bitmap to a DIB (Device Independent Bitmap) which he will later save to file in the Bitmap format. The recasting of pointers is a critical technique to this operation, because when he makes a call later in his program to WriteFile he just supplies the pointer as the parameter. Anyway, this is from the documantation at the MSDN site: C/C++ Language Reference reinterpret_cast Operator reinterpret_cast < type-id > ( expression ) The reinterpret_cast operator allows any pointer to be converted into any other pointer type. It also allows any integral type to be converted into any pointer type and vice versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired conversion is inherently low-level, you should use one of the other cast operators. I's appreciate any enlightenment on the subject that you can provide, as pointer casting appears to be an operation that could possibly be corrupted. I'm thinking that a special operator could be designed that would be better suited to these types of code transitions. -
I am a novice at programming and am working my way through Feng Yuan's book "Windows Graphics Programming". It's great stuff and he includes extensive development of useful graphics classes that can be used in your programs. Anyway, I'm curious as to why he doesn't use the reinterpret_cast operator in his code when converting pointers. Typically he does something like this:
// query GDI for image size GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, NULL, (BITMAPINFO *) & dibinfo, DIB_RGB_COLORS); int nInfoSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * GetDIBColorCount(dibinfo.bmiHeader); int nTotalSize = nInfoSize + GetDIBPixelSize(dibinfo.bmiHeader); BYTE * pDIB = new BYTE[nTotalSize]; if ( pDIB ) { memcpy(pDIB, & dibinfo, nInfoSize); if ( ddbinfo.bmHeight != GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, pDIB + nInfoSize, (BITMAPINFO *) pDIB, DIB_RGB_COLORS) ) { delete [] pDIB; pDIB = NULL; } } ReleaseDC(NULL, hDC); return (BITMAPINFO *) pDIB;
This is a segment from one of his functions that converts a Device Dependent Bitmap to a DIB (Device Independent Bitmap) which he will later save to file in the Bitmap format. The recasting of pointers is a critical technique to this operation, because when he makes a call later in his program to WriteFile he just supplies the pointer as the parameter. Anyway, this is from the documantation at the MSDN site: C/C++ Language Reference reinterpret_cast Operator reinterpret_cast < type-id > ( expression ) The reinterpret_cast operator allows any pointer to be converted into any other pointer type. It also allows any integral type to be converted into any pointer type and vice versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired conversion is inherently low-level, you should use one of the other cast operators. I's appreciate any enlightenment on the subject that you can provide, as pointer casting appears to be an operation that could possibly be corrupted. I'm thinking that a special operator could be designed that would be better suited to these types of code transitions.As far as I know, reinterpret_cast isn't anything different from a class C-style cast. You can basically turn any pointer into anything else either way. I'm not sure what you would want with this special operator? But you might want to take a look at dynamic_cast, that might be the one you're looking for. But that only works with polymorphism, and that doesn't work for the WINAPI stuff.
-
As far as I know, reinterpret_cast isn't anything different from a class C-style cast. You can basically turn any pointer into anything else either way. I'm not sure what you would want with this special operator? But you might want to take a look at dynamic_cast, that might be the one you're looking for. But that only works with polymorphism, and that doesn't work for the WINAPI stuff.
Thanks for the response. I should have been clearer in my initial question. The code that Feng Yuan uses works perfectly, but appears cumbersome and even though the compiler will accept the cast, it does no checking of types to insure adequate memory addressing (...but, I'm probably wrong about this). In this case, Feng Yuan casts from a BYTE pointer to a BITMAPINFO pointer (they obviously are the same size or the function would not work). I have not tried this, but, I'm guessing that you could use the same technique with a VOID* (VOID pointer). More importantly, why even use reinterpret_cast at all? Why does Microsoft even include it in it's Programmer API? This whole subject is a mystery to me. I'm curious as to what instructions the compiler actually issues when such a cast is made. Also, an assert statement could be made, but Feng Yuan never does. No doubt I'm missing something important here. This further information from MSDN: There are several casting operators specific to the C++ language. These operators are intended to remove some of the ambiguity and danger inherent in old style C language casts. These operators are: dynamic_cast Used for conversion of polymorphic types. static_cast Used for conversion of nonpolymorphic types. const_cast Used to remove the const, volatile, and __unaligned attributes. reinterpret_cast Used for simple reinterpretation of bits.
-
Thanks for the response. I should have been clearer in my initial question. The code that Feng Yuan uses works perfectly, but appears cumbersome and even though the compiler will accept the cast, it does no checking of types to insure adequate memory addressing (...but, I'm probably wrong about this). In this case, Feng Yuan casts from a BYTE pointer to a BITMAPINFO pointer (they obviously are the same size or the function would not work). I have not tried this, but, I'm guessing that you could use the same technique with a VOID* (VOID pointer). More importantly, why even use reinterpret_cast at all? Why does Microsoft even include it in it's Programmer API? This whole subject is a mystery to me. I'm curious as to what instructions the compiler actually issues when such a cast is made. Also, an assert statement could be made, but Feng Yuan never does. No doubt I'm missing something important here. This further information from MSDN: There are several casting operators specific to the C++ language. These operators are intended to remove some of the ambiguity and danger inherent in old style C language casts. These operators are: dynamic_cast Used for conversion of polymorphic types. static_cast Used for conversion of nonpolymorphic types. const_cast Used to remove the const, volatile, and __unaligned attributes. reinterpret_cast Used for simple reinterpretation of bits.
I can't tell you as to why using reinterpret_cast instead of C-style casting. Basically it comes down to the same as far as I know, and you could use it for the sake of consistancy. Like if you use stuff like dynamic_cast, static_cast, etc. it would be more consistant to use reinterpret_cast as well. Maybe this link can enlighten you more: http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=131
-
I am a novice at programming and am working my way through Feng Yuan's book "Windows Graphics Programming". It's great stuff and he includes extensive development of useful graphics classes that can be used in your programs. Anyway, I'm curious as to why he doesn't use the reinterpret_cast operator in his code when converting pointers. Typically he does something like this:
// query GDI for image size GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, NULL, (BITMAPINFO *) & dibinfo, DIB_RGB_COLORS); int nInfoSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * GetDIBColorCount(dibinfo.bmiHeader); int nTotalSize = nInfoSize + GetDIBPixelSize(dibinfo.bmiHeader); BYTE * pDIB = new BYTE[nTotalSize]; if ( pDIB ) { memcpy(pDIB, & dibinfo, nInfoSize); if ( ddbinfo.bmHeight != GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, pDIB + nInfoSize, (BITMAPINFO *) pDIB, DIB_RGB_COLORS) ) { delete [] pDIB; pDIB = NULL; } } ReleaseDC(NULL, hDC); return (BITMAPINFO *) pDIB;
This is a segment from one of his functions that converts a Device Dependent Bitmap to a DIB (Device Independent Bitmap) which he will later save to file in the Bitmap format. The recasting of pointers is a critical technique to this operation, because when he makes a call later in his program to WriteFile he just supplies the pointer as the parameter. Anyway, this is from the documantation at the MSDN site: C/C++ Language Reference reinterpret_cast Operator reinterpret_cast < type-id > ( expression ) The reinterpret_cast operator allows any pointer to be converted into any other pointer type. It also allows any integral type to be converted into any pointer type and vice versa. Misuse of the reinterpret_cast operator can easily be unsafe. Unless the desired conversion is inherently low-level, you should use one of the other cast operators. I's appreciate any enlightenment on the subject that you can provide, as pointer casting appears to be an operation that could possibly be corrupted. I'm thinking that a special operator could be designed that would be better suited to these types of code transitions.reinterpret_cast
is the same as the C style cast your writer uses. The compiler does nothing, the cast is a statement from you the programmer that the types are equivalent. In this case they are. It would be better style to usereinterpret_cast
but the code in question isn't modern C++, rather it's C with a few C++ bits, so the C style class seems appropriate. The other C++ casts are more constraining and allow the compiler to determine whether what you (or a user of your code) are trying to do is appropriate. Paul -
reinterpret_cast
is the same as the C style cast your writer uses. The compiler does nothing, the cast is a statement from you the programmer that the types are equivalent. In this case they are. It would be better style to usereinterpret_cast
but the code in question isn't modern C++, rather it's C with a few C++ bits, so the C style class seems appropriate. The other C++ casts are more constraining and allow the compiler to determine whether what you (or a user of your code) are trying to do is appropriate. PaulPaul, thanks, that makes alot of sense. I appreciate the response. I don't know why, but I've always had this irrational fear of rogue pointers, since it's so easy to re-assign the address. But, they sure are ubiquitous, and so many Windows functions couldn't live without them.
-
Thanks for the response. I should have been clearer in my initial question. The code that Feng Yuan uses works perfectly, but appears cumbersome and even though the compiler will accept the cast, it does no checking of types to insure adequate memory addressing (...but, I'm probably wrong about this). In this case, Feng Yuan casts from a BYTE pointer to a BITMAPINFO pointer (they obviously are the same size or the function would not work). I have not tried this, but, I'm guessing that you could use the same technique with a VOID* (VOID pointer). More importantly, why even use reinterpret_cast at all? Why does Microsoft even include it in it's Programmer API? This whole subject is a mystery to me. I'm curious as to what instructions the compiler actually issues when such a cast is made. Also, an assert statement could be made, but Feng Yuan never does. No doubt I'm missing something important here. This further information from MSDN: There are several casting operators specific to the C++ language. These operators are intended to remove some of the ambiguity and danger inherent in old style C language casts. These operators are: dynamic_cast Used for conversion of polymorphic types. static_cast Used for conversion of nonpolymorphic types. const_cast Used to remove the const, volatile, and __unaligned attributes. reinterpret_cast Used for simple reinterpretation of bits.
According to Stroustrup[^], part of the reason for adding the various cast operators and making them so syntactically ugly was to make them more noticeable in your code. It's difficult to tell the difference between a 'C'-style cast and a user-defined conversion operator. The '*_cast' operators make those sorts of things easier to see, and more explicit of programmer intent.
Software Zen:
delete this;