|
Boost : |
From: John Max Skaller (skaller_at_[hidden])
Date: 2001-07-03 11:54:46
Darin Adler wrote:
>
> On Sunday, July 1, 2001, at 09:35 AM, John Max Skaller wrote:
>
> > occasionally: buffers in copyable objects should usually
> > use unsigned char, since arrays of char can't be copied.
>
> Could you elaborate on this? I've never heard before that arrays of char
> can't be copied, but arrays of unsigned char can be. Is this really what
> you meant to say?
Yes. The following is not well defined:
struct X {
char a[20];
};
X x;
X y = x;
The reason is that the array is copied member wise, char by
char, and copying an uninitialised char is undefined behaviour,
just like float. The ONLY (primitive) type you can use uninitialised
is unsigned char (which has an unspecified value, but the value
is guarranteed to be a valid unsigned char).
Note that a 'memcpy' of course will always work,
so the following is OK:
struct X {
char a[20];
X(const X &y) { memcpy(y.a,a,20); }
};
X x; X y = x; // OK
Note that of course, copying chars works on most machines,
it just isn't guarranteed by either ISO C++ or ISO C.
-- John (Max) Skaller, mailto:skaller_at_[hidden] 10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850 New generation programming language Felix http://felix.sourceforge.net Literate Programming tool Interscript http://Interscript.sourceforge.net
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk