🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4837

Copying structures

#4837
From: James Kanze US/ESC 60/3/141 #407...
Date: Wed, 28 Feb 1996 09:59
56 lines
2095 bytes
I seem to recall hearing that there was a technical correction or a
clarification concerning this.  Could anyone confirm this.

Is the following program well defined:

	#include <string.h>

	struct S { char s[ 20 ] ; } ;

	struct S
	f()
	{
	    struct S        s ;
	    strcpy( s.s , "abc" ) ;
	    return s ;
	}

I am particularly concerned about the fact that copying the structure
out as a return value involves copying (reading) uninitialized data.

On a similar vein, using the same struct:

	void
	f( FILE* fp )
	{
	    struct S        s ;
	    strcpy( s.s , "abc" ) ;
	    fwrite( &s , sizeof( struct S ) , 1 , fp ) ;
	}

Is this guaranteed not to invoke undefined behavior?  (It is
interesting to note that Purify detects an error -- uninitialized
memory read -- in this case.)

(Note that in both cases, I don't really care what the compiler does
with the uninitialized values.  It can change them in any way it
likes.)

Note that while I can hardly imagine an implementation where this
fails, if the struct is changed to something like:

	struct MultiDimPoint
	{
	    int             numDims ;
	    double          dims[ MAXDIMS ] ;
	} ;

It is not hard to conceive of implementations where this would fail.
(The compiler generates a loop using floating point loads and stores
to copy the doubles, the machine uses IEEE format, and the bit pattern
in one of the uninitialized doubles is a signaling NaN.)
-- 
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
                -- A la recherche d'une activité dans une region francophone
Technical Details
Message-ID: <KANZE.96Feb28105936@slsvewt.lts.sel.alcatel.de>
Path: arc1-ca.pugleaf.net!blackbush.xlink.net!slsv6bt!news

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!blackbush.xlink.net!slsv6bt!news
From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Subject: Copying structures
Message-ID: <KANZE.96Feb28105936@slsvewt.lts.sel.alcatel.de>
Sender: news@lts.sel.alcatel.de
Organization: GABI Software, Sarl.
Date: 28 Feb 1996 09:59:35 GMT