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
|
|
0
|
|
|
|
Reply
|
Tao
|
8/8/2003 3:51:53 PM |
|
"Tao Zhang" <taoz@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
mru@users.sf.net
|
|
0
|
|
|
|
Reply
|
mru
|
8/8/2003 3:58:48 PM
|
|
On Fri, 08 Aug 2003 15:51:53 GMT, Tao Zhang <taoz@earthlink.net> wrote:
> 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
>
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
|
|
0
|
|
|
|
Reply
|
Marc
|
8/8/2003 4:09:40 PM
|
|
Tao Zhang <taoz@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.Toerring@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
|
|
0
|
|
|
|
Reply
|
Jens
|
8/8/2003 4:14:24 PM
|
|
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
>
> 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
>
>
>
>
|
|
0
|
|
|
|
Reply
|
Tao
|
8/8/2003 4:15:33 PM
|
|
Got it. Thanks a lot, Jens.
Best Regards
Tao
> Tao Zhang <taoz@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.Toerring@physik.fu-berlin.de
> _ | | | | | |
> | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
> \___/ens|_|homs|_|oerring
>
|
|
0
|
|
|
|
Reply
|
Tao
|
8/8/2003 4:18:54 PM
|
|
"Tao Zhang" <taoz@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)
|
|
0
|
|
|
|
Reply
|
Juha
|
8/8/2003 5:22:00 PM
|
|
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
|
|
|
|
Reply
|
Allin
|
8/11/2003 1:41:21 PM
|
|
|
7 Replies
1168 Views
(page loaded in 0.203 seconds)
Similiar Articles: how to ask GCC to automatically initialize local variables - comp ...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 pr... local variables - comp.text.texhow to ask GCC to automatically initialize local variables - comp ... Hi, all How can I ask GCC to automatically initialize local variables? I think some compilers do that ... lcc or gcc ,which better? - comp.compilers.lcchow to ask GCC to automatically initialize local variables - comp ... lcc or gcc ,which better? - comp.compilers.lcc how to ask GCC to automatically initialize local ... Allocatable versus automatic arrays - comp.lang.fortranhow to ask GCC to automatically initialize local variables - comp ... Allocatable versus automatic arrays - comp.lang.fortran It seems that many can efficiently initialize ... Something bad happened to my gfortran installation - comp.lang ...how to ask GCC to automatically initialize local variables - comp ... Something bad happened to my gfortran installation - comp.lang ..... equation.com, gcc-4.6-20101113 ... Renaming host with Oracle 10g - comp.unix.solarisI actually had to mv the local.ocr file out of the ... 4.Set the ORACLE_HOME environment variable to specify ... When the system boots, the CSS daemon starts automatically ... improve strlen - comp.lang.asm.x86... tried write my pesonal routine faster than gcc ... You initialize the string's length once. You use it many ... operations and very small number of local variables, the ... Need a FORTRAN compiler for Win7 (or XP) - comp.lang.fortran ...See also http://gcc.gnu.org/wiki ... automatic (if the program assumes that all local variables ... I can even automatically re-structure such a program ... getting cpan configured - comp.unix.shellIf the environment variable NNTPSERVER is not set, then ... You'll get slaughtered even worse if you ask the ... configure Serial port - comp.unix.solaris How to AUTOMATICALLY ... Bad use of stringstream temporary? - comp.lang.c++... value, while the version with the named local variable works ... return a reference to non-const, and you can initialize ... respond to top-posted replies, please don't ask how to ask GCC to automatically initialize local variables - comp ...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 pr... c - Does gcc automatically initialize static variables to zero ...Does gcc automatically initialize static variables to zero? ... other questions tagged c or ask ... Is there a gcc flag to initialise local variable storage? 7/20/2012 5:29:13 AM
|