19

The IBM AIX xlc compiler offers a flag that generates code to initialise local variable storage:

      initauto=<hh>
                  Initialialize automatic storage to <hh>. <hh> is a
                  hexadecimal value.  This generates extra code and
                  should only be used for error determination.

I think the MSVC compiler does something similar for debug builds, but my memory may be hazy on this point.

Is there an equivalent option for GCC?

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • `-Wextra` is great for new code bases that don't already have 3 million lines of code. :) – Greg Hewgill May 10 '12 at 02:47
  • 1
    I can't believe initialize is spelled "initialialize" in the compiler docs. – Andrew Marshall May 10 '12 at 02:48
  • @AndrewMarshall: Ha! I didn't even notice that. It really is spelled that way, although that xlc is quite old. The current [online help](http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Fcompiler%2Fref%2Fruoptini.htm) doesn't have that error. – Greg Hewgill May 10 '12 at 02:51
  • I dunno, I kinda think `-Wextra` is even better for ratty, nasty old codebases that already have a few million lines of code :-) Who needs TDD then? O:-) – BRPocock May 10 '12 at 02:53

3 Answers3

10

OK, Best answer I can offer.

http://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html says "no," by omission. There's no documentation of anything to inject stack-wiping code into the output.

As near as I could guess, the only way this could work, is to inject some memset-like code (perhaps as simple as a few mov operations, but nonetheless) into the beginning of each embedded lexical frame in which an automatic variable is created. As near as I can tell -- and I am far from an expert on the internals of GCC, but -- there seems to be nothing documented that would do so.

In further following this, the PDF gccint.pdf of GCC Internals (http://gcc.gnu.org/onlinedocs/gccint.pdf) on page 361 defines that the GCC name for the frame pointer adjustment call step on entry to a function is prologue. (I don't really know/understand whether this applies to other lexical scopes within a function, however.) Since that should occur in a Machine Definition (md) file, any such option would seem to have to be defined for a CPU architecture. I poked at their online ViewCVS at http://gcc.gnu.org/viewcvs/trunk/gcc/config/i386/ and found (at least one) copy of prologue around line 11,893 of i386.md, which after playing search-for-the-function-expansion a few hops, doesn't seem to have anything to emit conditional code like that.

But this under-GCC's-hood stuff is kinda neat...

BRPocock
  • 13,638
  • 3
  • 31
  • 50
  • Thanks, this looks like the closest to the (currently) correct answer. There are undoubtedly many possible details involved in an actual implementation, such as C99 variable length arrays, re-use of local variable space, and things like `alloca()`. – Greg Hewgill May 10 '12 at 06:27
3

I cannot find any definitive reference, but it seems that certain copies of GCC (particularly the GCC Fortran compiler) have a -finit-local-zero option to automatically set any non-explicitly initialized local variables or arrays to zero.

As far standard GCC goes, the only feature on this topic that I could find is -Wuninitialized to throw warnings on any uninitialized variables (though I know this isn't what you're looking for).

How badly do you need this? If you have a really good reason, I suppose it can't be that hard to copy the -finit-local-zero code to your version of GCC...

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • It's kinda localised to the FORTRAN front-end, however: http://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html ... nice find, but I'm afraid it wouldn't help with C-based code – BRPocock May 10 '12 at 03:28
  • 2
    This isn't a huge need at the moment. The idea was that if we turned such an option on for xlc (with this 3+ million line code base), and then migrated to gcc later, the code might be relying on definite initialisation which wouldn't happen without a corresponding option in gcc. Ideally, the long-term solution is to use `-Wuninitalized` with `-Werror` (xlc has equivalent options for that too). – Greg Hewgill May 10 '12 at 06:30
-1

C99: If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

There has been a topic about this question. What happens to a declared, uninitialized variable in C? Does it have a value?.

Community
  • 1
  • 1
  • 8
    I understand the language specification does not require initialisation of variables with automatic storage duration. However, at least one compiler has an *option* to do so if you ask it to. My question is whether GCC has a similar option. – Greg Hewgill May 10 '12 at 02:44