Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Uninitialized auto variables (http://www.velocityreviews.com/forums/t525644-uninitialized-auto-variables.html)

Spoon 07-30-2007 08:09 AM

Uninitialized auto variables
 
Hello everyone,

I suppose using uninitialized automatic integer variables leads
to undefined behavior?

i.e.

int foo(void)
{
int bar; /* bar may be 0, or it may be non-0 */
return bar;
}

I sometimes do that when I need an int, any int.

I suppose this is a bad habit? :-)

Regards.

Chris Dollin 07-30-2007 08:16 AM

Re: Uninitialized auto variables
 
Spoon wrote:

> Hello everyone,
>
> I suppose using uninitialized automatic integer variables leads
> to undefined behavior?


Yes.

> int foo(void)
> {
> int bar; /* bar may be 0, or it may be non-0 */
> return bar;
> }
>
> I sometimes do that when I need an int, any int.
>
> I suppose this is a bad habit? :-)


Very.

(If "any int" will do, zero is a convenient value.)

--
Far-Fetched Hedgehog
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/


santosh 07-30-2007 08:46 AM

Re: Uninitialized auto variables
 
Spoon wrote:

> Hello everyone,
>
> I suppose using uninitialized automatic integer variables leads
> to undefined behavior?


Yes.

> i.e.
>
> int foo(void)
> {
> int bar; /* bar may be 0, or it may be non-0 */
> return bar;
> }
>
> I sometimes do that when I need an int, any int.
>
> I suppose this is a bad habit? :-)


Yes. If you need a random integer, just use the rand Standard library
function.

man 3 rand


Keith Thompson 07-30-2007 08:53 AM

Re: Uninitialized auto variables
 
Spoon <root@localhost> writes:
> I suppose using uninitialized automatic integer variables leads
> to undefined behavior?
>
> i.e.
>
> int foo(void)
> {
> int bar; /* bar may be 0, or it may be non-0 */
> return bar;
> }
>
> I sometimes do that when I need an int, any int.
>
> I suppose this is a bad habit? :-)


Yes.

What are the circumstances in which you need "an int, any int" but
don't care about the value?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Spoon 07-30-2007 09:16 AM

Re: Uninitialized auto variables
 
Keith Thompson wrote:

> Spoon wrote:
>
>> I suppose using uninitialized automatic integer variables leads
>> to undefined behavior?
>>
>> i.e.
>>
>> int foo(void)
>> {
>> int bar; /* bar may be 0, or it may be non-0 */
>> return bar;
>> }
>>
>> I sometimes do that when I need an int, any int.
>>
>> I suppose this is a bad habit? :-)

>
> Yes.
>
> What are the circumstances in which you need "an int, any int" but
> don't care about the value?


For example, an opaque identifier. Any value will do.

I suppose rand() is a much better option.

Richard Tobin 07-30-2007 09:29 AM

Re: Uninitialized auto variables
 
In article <46adac8b$0$28621$426a74cc@news.free.fr>,
Spoon <root@localhost> wrote:

>>> int foo(void)
>>> {
>>> int bar; /* bar may be 0, or it may be non-0 */
>>> return bar;
>>> }
>>>
>>> I sometimes do that when I need an int, any int.


>> What are the circumstances in which you need "an int, any int" but
>> don't care about the value?


>For example, an opaque identifier. Any value will do.


When do you need opaque identifiers but not care about whether they're
different? Why not just use "45576" instead of "foo()"?

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Spoon 07-30-2007 09:53 AM

Re: Uninitialized auto variables
 
Richard Tobin wrote:
> In article <46adac8b$0$28621$426a74cc@news.free.fr>,
> Spoon <root@localhost> wrote:
>
>>>> int foo(void)
>>>> {
>>>> int bar; /* bar may be 0, or it may be non-0 */
>>>> return bar;
>>>> }
>>>>
>>>> I sometimes do that when I need an int, any int.

>
>>> What are the circumstances in which you need "an int, any int" but
>>> don't care about the value?

>
>> For example, an opaque identifier. Any value will do.

>
> When do you need opaque identifiers but not care about whether they're
> different? Why not just use "45576" instead of "foo()"?


I only needed one identifier per run.
(It didn't matter if it was the same identifier on the next run.)
I won't do it anymore, now that I know it leads to UB.

Mark Bluemel 07-30-2007 09:57 AM

Re: Uninitialized auto variables
 
Spoon wrote:
> Richard Tobin wrote:
>
>> In article <46adac8b$0$28621$426a74cc@news.free.fr>,
>> Spoon <root@localhost> wrote:


>>> ... an opaque identifier. Any value will do.

>>
>>
>> When do you need opaque identifiers but not care about whether they're
>> different? Why not just use "45576" instead of "foo()"?

>
> I only needed one identifier per run.


If there's only one, why do you need it at all? I don't need to be able
to differentiate myself from myself.

CBFalconer 07-30-2007 10:18 AM

Re: Uninitialized auto variables
 
Richard Tobin wrote:
> Spoon <root@localhost> wrote:
>

.... snip ...
>
>> For example, an opaque identifier. Any value will do.

>
> When do you need opaque identifiers but not care about whether
> they're different? Why not just use "45576" instead of "foo()"?


For one reason, because the guaranteed minimum maximm integer value
is 32767.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net


--
Posted via a free Usenet account from http://www.teranews.com


Pietro Cerutti 07-30-2007 10:32 AM

Re: Uninitialized auto variables
 
santosh wrote:
> Spoon wrote:
>
>> Hello everyone,
>>
>> I suppose using uninitialized automatic integer variables leads
>> to undefined behavior?

>
> Yes.

Could you please point me to the paragraph of the standard where it's
told to be so?

I can only find this sentence in WG14/N1124, 6.2.4.5

"...The initial value of the object is indeterminate...."

Which doesn't mean, to my understanding, that using it invokes UB.

Thanks for clarifying this point!

>
>> i.e.
>>
>> int foo(void)
>> {
>> int bar; /* bar may be 0, or it may be non-0 */
>> return bar;
>> }
>>
>> I sometimes do that when I need an int, any int.
>>
>> I suppose this is a bad habit? :-)

>
> Yes. If you need a random integer, just use the rand Standard library
> function.
>
> man 3 rand
>



--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp


All times are GMT. The time now is 09:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.