Re: Uninitialised Unsigned Integral == Okay
"Tom1s" wrote:
Seungbeom Kim posted:
And what does that buy you? What useful things could you do
with values of uninitialized objects?
struct ManufacturerInfo {
bool has_id;
unsigned long id;
};
ManufacturerInfo SomeFunc()
{
ManufacturerInfo info;
/* Let's try to retrieve their ID from a database.
And let's assume that the retrieval failed.
*/
info.has_id = false;
return info;
}
If there were no invalid bit patterns, the above function
wouldn't have to worry about the calling function accessing
the "id" member. Of course, "SomeFunc" could simply set it to
zero, but that would be inefficient.
That's actually an interesting example. The above code has
undefined behavior, regardless of what the user code does, since
you cannot copy an uninitialized value (in theory, and except
with memcpy and the like.)
And while I don't know about this exact example, I have had
problems in the past with code like:
struct
{
char name[ 20 ] ;
char id[ 15 ] ;
// ...
} ;
being used for disk layout, and the fields being initialized
using strcpy. I got error messages from the system about
reading uninitialized memory.
--
James Kanze GABI Software
Conseils en informatique orient9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S9mard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
---
[ 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.comeaucomputing.com/csc/faq.html ]