I'm trying to avoid adopting bad habits as I learn to program in C++. I've read that reinterpret_cast is best avoided because it turns one type directly into another, which may cause unexpected results, especially when compiling and running the program on a system with a different architecture that might internally store the affected types differently. Maybe there are other reasons too, but that seemed to be the danger most often called out in the references I found.
In any case, I'm using it right now in the following situation. I'd like to know if there is a way I could do this that doesn't involve using reinterpret_cast, and also whether it is known to be dangerous in this usage case.
The translate function works with unsigned char *; it treats each character of a buffer that is passed to it as an index in the range of 0 to 255 to access a 256 byte array of translated characters.
As far as I know, I have to define the buffer as unsigned char * and reinterpret_cast when I use it in the fstream read statement, or define it as char * type and reinterpret_cast to unsigned char * when I use it in the translate function.
This is a simplified code fragment for illustrative purposes, not a complete working example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
void translate(size_t, unsigned char *, unsigned char *);
int main() {
fstream fs; // an fstream
unsigned char *fsbuf; // pointer to char buffer
unsigned char *table; // pointer to translation table
size_t buflen; // length of data to translate
... (code here to open file in binary mode)
fs.read(reinterpret_cast<char*>(fsbuf), 4096);
buflen = fs.gcount();
translate(buflen, fsbuf, table);
... (more code)
}
void translate(size_t len, unsigned char *s, unsigned char *trt)
{ // translate text of length len in buffer s using table trt.
register unsigned char *buf;
register int j;
for (buf=s, j=len; j; j--, buf++)
*buf = trt[*buf];
return;
}
|
Since a value of type char and one of type unsigned char both occupy one byte, does this use of reinterpret_cast pose a problem? What if a char and an unsigned char are not the same length on some systems?
Is there some way I can avoid the reinterpret_cast in my example?