Angus <> writes:
> On Jan 28, 10:04Â*pm, "Scott Fluhrer" <sfluh...@ix.netcom.com> wrote:
[...]
>> Another real possibility is that it works as shown on 99% of the runs, but
>> prints gibberish on the other 1%. Â*This can happen on real computers that
>> use the same stack to hold both automatics and interrupt contexts.
>>
>> This is Yet Another reason to avoid undefined behavior; just because your
>> program works once does not mean that it'll work the next time; even if
>> you'll rerun the exact same program...
>
> I believe it IS ok to return a static variable.
It's not *possible* to return a static variables. As someone already
explained upthread, functions do not return variables; they return
values.
A function may return the *value* of a variable, and that's perfectly ok
(as long as that value doesn't contain a pointer to a local object). Or
it may return the *address* of a variable, which is ok if and only if
the variable's lifetime doesn't end when the current invocation of the
function does.
So what you probably *meant* to say is correct: it's ok for a function
to return the address of a static variable, because such a variable's
lifetime is the entire execution of the program. (But making a variable
static changes its behavior, which may or may not be what you want.)
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"