This has been bugging me for a very long time: how to do pointer conversion from anything to char *
to dump binary to disk.
In C, you don't even think about it.
double d = 3.14;
char *cp = (char *)&d;
// do what u would do to dump to disk
However, in C++, where everyone is saying C-cast is frowned upon, I've been doing this:
double d = 3.14;
auto cp = reinterpret_cast<char *>(&d);
Now this is copied from cppreference, so I assume this is the proper way.
However, I've read from multiple sources saying this is UB. (e.g. this one) So I can't help wonder if there is any "DB" way at all (According to that post, there's none).
Another scenario I often encounter is to implement an API like this:
void serialize(void *buffer);
where you would dump a lot of things to this buffer. Now, I've been doing this:
void serialize(void *buffer) {
int intToDump;
float floatToDump;
int *ip = reinterpret_cast<int *>(buffer);
ip[0] = intToDump;
float *fp = reinterpret_cast<float *>(&ip[1]);
fp[0] = floatToDump;
}
Well, I guess this is UB as well.
Now, is there truly no "DB" way to accomplish either of these tasks?
I've seen someone using uintptr_t
to accomplish sth similar to serialize
task with pointer as integer math along with sizeof
,
but I'm guessing here that it's UB as well.
Even though they are UB, compiler writers usually do the rational things to make sure everything is okay. And I'm okay with that: it's not an unreasonable thing to ask for.
So my questions really are, for the two common tasks mentioned above:
- Is there truly no "DB" way to accomplish them that will satisfy the ultimate C++ freaks?
- Any better way to accomplish them other than what I've been doing?
Thanks!