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 );
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
Now, I've tried to *reproduce* the warning, with the following code:
int add(); // { return 666; }
void remove( int ); // {}
int foo( int ); // {}
struct S
{
virtual int bar()
{
int const id = add();
try
{
return foo( id );
}
catch( ... )
{
remove( id );
throw;
}
}
};
int main()
{
S().bar();
}
But this code does not produce the warning.
What is it that the compiler sees that I don't see?
Cheers & TIA.,
- Alf
How can a const int be uninitialised?
--
Ian Collins
> > the compiler complains that
How can any variable whose definition contains an initializer be
used uninitialised?
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Which line?
And why do use such an old compiler, don't they already have v4.x? Of
course, I can be mistaken, you've only posted a fragment, but 4.2 does
not produce any errors in this:
#include <string>
unsigned foo(std::string& s)
{
const int id = s.append(" ").size();
try
{
return s.size();
}
catch(...)
{
return id;
}
}
(except about the missing 'main').
>
> Now, I've tried to *reproduce* the warning, with the following code:
>
> int add(); // { return 666; }
> void remove( int ); // {}
> int foo( int ); // {}
>
> struct S
> {
> virtual int bar()
> {
> int const id = add();
> try
> {
> return foo( id );
> }
> catch( ... )
> {
> remove( id );
> throw;
> }
> }
> };
>
> int main()
> {
> S().bar();
> }
>
> But this code does not produce the warning.
>
> What is it that the compiler sees that I don't see?
<shrug> How do you know somebody else's hallucinations?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
// 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. The
code in the catch clause won't be reached, but it uses id. The compiler
might contain a bug, so that it wrongly traces the usage of id and
outputs this warning.
In your toy example, the add function does never throw, so the compiler
doesn't warn.
Of course, this is only a theory. You are free to prove or disprove it ;-)
--
Thomas
The reported line number points to the declaration of id.
> And why do use such an old compiler, don't they already have v4.x?
No, this is under Windows.
However there's a 4.4.1 version of g++ for Windows available from TDM, <url:
http://www.tdragon.net/recentgcc/>.
"The TDM-GCC builds are unofficial replacements for the official MinGW releases
of GCC binaries. TDM-GCC was previously recommended for experimentation purposes
only, but constant use in day-to-day development and a total download count of
over 50,000 have proven the TDM-GCC releases to be at least as usable as the
most recent official MinGW GCC release. Therefore, TDM-GCC is now heartily
endorsed for production use in any non-critical environment, with only the
following caveats: "
But it's a bit of a risk to use an "unofficial" in-advance build of a voluntary
working-for-free team's porting efforts of this free compiler.
I don't think it would solve anything really.
Cheers,
- Alf
It can't be uninitialised, it's a const.
--
Ian Collins
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