Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to ask GCC to automatically initialize local variables

2,177 views
Skip to first unread message

Tao Zhang

unread,
Aug 8, 2003, 11:51:53 AM8/8/03
to
Hi, all

How can I ask GCC to automatically initialize local variables?
I think some compilers do that and some not, which will lead to
portability problem. For example, I think GCC in Linux system
doesn't do that.

I know G77 has an option -f-init-local-zero to achieve this, how
about GCC?

Thanks in advance!

Tao


Måns Rullgård

unread,
Aug 8, 2003, 11:58:48 AM8/8/03
to
"Tao Zhang" <ta...@earthlink.net> writes:

> How can I ask GCC to automatically initialize local variables?
> I think some compilers do that and some not, which will lead to
> portability problem. For example, I think GCC in Linux system
> doesn't do that.

The C standard says local variables have undefined initial value. You
should always initialize them yourself, if you want portability.

--
Måns Rullgård
m...@users.sf.net

Marc Rochkind

unread,
Aug 8, 2003, 12:09:40 PM8/8/03
to

Asking gcc to do it would lead to a portability problem. Forgetting about
the ability of any compiler to do it eliminates the portability problem.

In other words... don't do it! This would apply to any compiler optin that
changes the semantics of the language being compiled.

--Marc

Jens.T...@physik.fu-berlin.de

unread,
Aug 8, 2003, 12:14:24 PM8/8/03
to
Tao Zhang <ta...@earthlink.net> wrote:
> How can I ask GCC to automatically initialize local variables?
> I think some compilers do that and some not, which will lead to
> portability problem. For example, I think GCC in Linux system
> doesn't do that.

As far as I know there's no such option. And the portability
problem actually is to assume that non-static local variables
will get initialized to zero - the C standard don't require it
and many compilers don't do it (in most cases it would just slow
down the program because on each function invocation the stack
would have to be zeroed without it being useful). So better check
carefully;-) The -Wuninitialized option (together with -O, other-
wise it won't work at all) might help you find such bugs in some
cases.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Jens.T...@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring

Tao Zhang

unread,
Aug 8, 2003, 12:15:33 PM8/8/03
to
Hi,

Thanks a lot for the quick help. The thing is that I always initialize
variables before using them, but some programs I am using don't.
That leads to tricky bugs.

So can GCC warn about the usage of uninitialized variables?
I just tried -Wall of GCC2.96, seems not work?

Thanks!

Tao

Tao Zhang

unread,
Aug 8, 2003, 12:18:54 PM8/8/03
to
Got it. Thanks a lot, Jens.

Best Regards

Tao

Juha Laiho

unread,
Aug 8, 2003, 1:22:00 PM8/8/03
to
"Tao Zhang" <ta...@earthlink.net> said:
>Thanks a lot for the quick help. The thing is that I always initialize
>variables before using them, but some programs I am using don't.
>That leads to tricky bugs.
>
>So can GCC warn about the usage of uninitialized variables?
>I just tried -Wall of GCC2.96, seems not work?

I tried that as well, and was surprised when it didn't give the warning
(as I was certain that I had seen such warnings). Then I read the gcc
info pages, and noticed:
...
`-Wuninitialized'
An automatic variable is used without first being initialized.

These warnings are possible only in optimizing compilation,
because they require data flow information that is computed only
when optimizing. If you don't specify `-O', you simply won't get
these warnings.

These warnings occur only for variables that are candidates for
register allocation. Therefore, they do not occur for a variable
that is declared `volatile', or whose address is taken, or whose
size is other than 1, 2, 4 or 8 bytes. Also, they do not occur for
structures, unions or arrays, even when they are in registers.
...

There are some other conditions documented as well, so I suggest you to
read the document yourself.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Allin Cottrell

unread,
Aug 11, 2003, 9:41:21 AM8/11/03
to
Tao Zhang wrote:
> Hi,
>
> Thanks a lot for the quick help. The thing is that I always initialize
> variables before using them, but some programs I am using don't.
> That leads to tricky bugs.
>
> So can GCC warn about the usage of uninitialized variables?
> I just tried -Wall of GCC2.96, seems not work?

You've now got the answer from others: You have to invoke optimization
at some level for -Wuninitialized to work.

One addition: valgrind ( http://developer.kde.org/~sewardj/ ) is good
at detecting this sort of thing.

Allin Cottrell.

0 new messages