|
|
Log in / Subscribe / Register

Buried in warnings

Buried in warnings

Posted Nov 2, 2006 10:46 UTC (Thu) by Asebe8zu (subscriber, #24600)
In reply to: Buried in warnings by JoeBuck
Parent article: Buried in warnings

Couldn't you use the following to make it correct?

some_type* pointer;

if (some_condition_is_true()) {
   pointer = expensive_allocation_function();
   do_something_else();
   use_the_fine(pointer);
}
else
{
   do_something_else();
}


to post comments

Buried in warnings

Posted Nov 2, 2006 15:05 UTC (Thu) by etienne_lorrain@yahoo.fr (guest, #38022) [Link] (2 responses)

That is just what you did not want to do, for instance when do_something_else() will be inlined if it is called once...

Either you would replace:
{
if (some_condition_is_true()) {
some_type* pointer = expensive_allocation_function();
do_something_else();
use_the_fine(pointer);
} else {
do_something_else();
}
}

By (definning a new GCC extern_local keyword):
{
if (some_condition_is_true()) {
some_type* pointer = expensive_allocation_function();
}
do_something_else();
if (some_condition_is_true()) {
extern_local some_type* pointer;
use_the_fine(pointer);
}
}

Or by a new GCC attribute:
{
some_type* pointer __attribute__((used_if(some_condition_is_true())));

if (some_condition_is_true())
pointer = expensive_allocation_function();
do_something_else();
if (some_condition_is_true())
use_the_fine(pointer);
}

By the way, to kill "used if not initialised" warning, you do it by
the recognised: "variable = variable;" feature, not by "variable = 0;".

Buried in warnings

Posted Nov 2, 2006 16:44 UTC (Thu) by NAR (subscriber, #1313) [Link]

By the way, to kill "used if not initialised" warning, you do it by the recognised: "variable = variable;" feature, not by "variable = 0;".

I think this still hides the case when a new execution path is added and the variable is failed to get initialized in the new path. I guess the real solution is to initialize at declaration (i.e. with a constructor) and do whatever case handling needed in that constructor - altough I'm not sure it can be done easily in C.

Bye,NAR

Buried in warnings

Posted Nov 3, 2006 1:07 UTC (Fri) by nix (subscriber, #2304) [Link]

I hope that's an assertion. you can't verify that attribute in the general
case without not just solving the halting problem but foretelling the
future. :)

And in that case you've now moved the problem from bogus initializations
to bogus assertions. The problem remains.

(And it's insoluble in the general case, anyway. Hence GCC's use of the
word `may', to emphasise that FPs from this warning are a
quality-of-implementation issue, sure, but not a compiler bug.)


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds