Topic: [C++] bitset serialization+30 (1) 7295100, Fax: +30 (1) 7258520, url: http://www.forthnet.gr
Author: "Homer Meyer" <homer@cqg.com>
Date: 29 Aug 2002 17:12:14 GMT Raw View
"Ioannis Vranos" <noicys@spammers.get.lost.hotmail.com> wrote in message
news:1029417405.809194@athprx02.forthnet.gr...
>
> "James Russell Kuyper Jr." <kuyper@wizard.net> wrote in message
> news:3D58664F.A357A4CB@wizard.net...
> >
> > Ioannis Vranos wrote:
> > ..
> > > You can copy an object to a char array or file and then copy it
> back
> > > and have the same functionality. If by dumping you mean to
> consider
> > > the result as its bit contents, the answer is "no way".
> >
> > Only for POD objects, of course.
>
>
> Yes. However i think that you can copy any object to an unsigned char
> array and then copy it back and have the same functionality:
>
> "The object representation of an object of type T is the sequence of N
> unsigned char objects taken up by the object of type T, where N equals
> sizeof(T). The value representation of an object is the set of bits that
> hold the value of type T.".
If you are saying that something like this isn't undefined behavior, then I
think you should look again:
struct A {
int a;
vitrual ~A() {}
};
A* one = new A();
unsigned char buff[sizeof(A)];
memcpy(buff, one);
delete one;
one = (A*)(new unsigned char[sizeof(A)]);
memcpy(one, buff);
Ok, that isn't copying back to the same object. But even this produces
undefined behavior:
std::string b = "short";
unsigned char buff[sizeof(b)];
memcpy(buff, b);
b = "much longer string here";
memcpy(b,buff);
The point being, that you have to copy the bytes back to the original object
without doing any operations at all on the original object or modifying the
bytecopy between copying the bytes out and copying them back in, otherwise
there is a very good chance of invoking undefined behavior. It's not just
non-const operations that you must avoid. An object could have mutable data
members which can be modified by const operations.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]