Reinterpret_cast <int *> (char *) vs. static_cast <int *> (static_cast <void *> (char *)) - what to use?
When you dynamically allocate a buffer of type char *
and want to pass it to a specific type, you should use something like
reinterpret_cast<int *>(char *)
or something like
static_cast<int *>(static_cast<void *>(char *))
and why?
I personally am tempted to use the latter, because for me it is not really a “reinterpretation” of the data (rather, just a mechanical way to allocate a buffer), and it does not look like it will be a source of errors just like a typical reinterpret_cast
, but is this the right intuition ?
+4
1 answer
According to Dave Abraham , using chained static_cast
is the right standard way to force pointer types.
Personally, I use reinterpret_cast
in these cases because I never have to deal with architectures that will do one thing with chained static_cast
and the other with a single reinterpret_cast
.
+4