Topic: Undefined copying, was: Avoiding zero-initialization in containers of built-in types
Author: James.Kanze@dresdner-bank.com
Date: 1999/11/13 Raw View
In article <3829D761.FC5D91B5@quidsoft.se>,
Dag Henriksson <dag.henriksson@quidsoft.se> wrote:
>
>
> Andrei Alexandrescu wrote:
>
> > Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote in message
> > news:382705BA.A02@wanadoo.fr...
> > [Dave Abraham's code]
> > > > struct Int { int x; }
> > > > vector<Int> v(20);
> > >
> > He doesn't want undefined behavior. It's certainly about defined
behavior.
> > The initial values of the integers are undefined, not the behavior.
> Yes the behavior is undefined. The initialization will initalized the
> items in the vector with a default initialized struct Int. Since the
> member x doesn't get initialized in the code below the lvalue to
> rvalue conversion produces undefined behavior. (see 4.1 / 1) The code
> above is OK however.
That's the quote I was looking for. However, it seems stronger than
what I was looking for -- it doesn't seem to make an exception for char
and unsigned char, which means that formally, the following program
invokes undefined behavior:
struct S
{
char c[ 100 ] ;
} ;
S s1 ;
strcpy( s1.c , "x" ) ;
S s2 ;
unsigned char* dst = (unsigned char*)(void*)(&s2) ;
unsigned char* src = (unsigned char*)(void*)(&s1) ;
for ( int i = 0 ; i < sizeof( S ) ; ++ i ) {
dst[ i ] = src[ i ] ;
}
This is rather a pain, because it is (under various variants) a commun
idiom in C.
--
James Kanze mailto:James.Kanze@dresdner-bank.com
Conseils en informatique orient e objet/
Beratung in objekt orientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]