1*ec02198aSmrg@c Copyright (C) 1988-2020 Free Software Foundation, Inc.
210d565efSmrg@c This is part of the GCC manual.
310d565efSmrg@c For copying conditions, see the file gcc.texi.
410d565efSmrg
510d565efSmrg@node Trouble
610d565efSmrg@chapter Known Causes of Trouble with GCC
710d565efSmrg@cindex bugs, known
810d565efSmrg@cindex installation trouble
910d565efSmrg@cindex known causes of trouble
1010d565efSmrg
1110d565efSmrgThis section describes known problems that affect users of GCC@.  Most
1210d565efSmrgof these are not GCC bugs per se---if they were, we would fix them.
1310d565efSmrgBut the result for a user may be like the result of a bug.
1410d565efSmrg
1510d565efSmrgSome of these problems are due to bugs in other software, some are
1610d565efSmrgmissing features that are too much work to add, and some are places
1710d565efSmrgwhere people's opinions differ as to what is best.
1810d565efSmrg
1910d565efSmrg@menu
2010d565efSmrg* Actual Bugs::         Bugs we will fix later.
2110d565efSmrg* Interoperation::      Problems using GCC with other compilers,
2210d565efSmrg                        and with certain linkers, assemblers and debuggers.
2310d565efSmrg* Incompatibilities::   GCC is incompatible with traditional C.
2410d565efSmrg* Fixed Headers::       GCC uses corrected versions of system header files.
2510d565efSmrg                        This is necessary, but doesn't always work smoothly.
2610d565efSmrg* Standard Libraries::  GCC uses the system C library, which might not be
2710d565efSmrg                        compliant with the ISO C standard.
2810d565efSmrg* Disappointments::     Regrettable things we cannot change, but not quite bugs.
2910d565efSmrg* C++ Misunderstandings:: Common misunderstandings with GNU C++.
3010d565efSmrg* Non-bugs::            Things we think are right, but some others disagree.
3110d565efSmrg* Warnings and Errors:: Which problems in your code get warnings,
3210d565efSmrg                        and which get errors.
3310d565efSmrg@end menu
3410d565efSmrg
3510d565efSmrg@node Actual Bugs
3610d565efSmrg@section Actual Bugs We Haven't Fixed Yet
3710d565efSmrg
3810d565efSmrg@itemize @bullet
3910d565efSmrg@item
4010d565efSmrgThe @code{fixincludes} script interacts badly with automounters; if the
4110d565efSmrgdirectory of system header files is automounted, it tends to be
4210d565efSmrgunmounted while @code{fixincludes} is running.  This would seem to be a
4310d565efSmrgbug in the automounter.  We don't know any good way to work around it.
4410d565efSmrg@end itemize
4510d565efSmrg
4610d565efSmrg@node Interoperation
4710d565efSmrg@section Interoperation
4810d565efSmrg
4910d565efSmrgThis section lists various difficulties encountered in using GCC
5010d565efSmrgtogether with other compilers or with the assemblers, linkers,
5110d565efSmrglibraries and debuggers on certain systems.
5210d565efSmrg
5310d565efSmrg@itemize @bullet
5410d565efSmrg@item
5510d565efSmrgOn many platforms, GCC supports a different ABI for C++ than do other
5610d565efSmrgcompilers, so the object files compiled by GCC cannot be used with object
5710d565efSmrgfiles generated by another C++ compiler.
5810d565efSmrg
5910d565efSmrgAn area where the difference is most apparent is name mangling.  The use
6010d565efSmrgof different name mangling is intentional, to protect you from more subtle
6110d565efSmrgproblems.
6210d565efSmrgCompilers differ as to many internal details of C++ implementation,
6310d565efSmrgincluding: how class instances are laid out, how multiple inheritance is
6410d565efSmrgimplemented, and how virtual function calls are handled.  If the name
6510d565efSmrgencoding were made the same, your programs would link against libraries
6610d565efSmrgprovided from other compilers---but the programs would then crash when
6710d565efSmrgrun.  Incompatible libraries are then detected at link time, rather than
6810d565efSmrgat run time.
6910d565efSmrg
7010d565efSmrg@item
7110d565efSmrgOn some BSD systems, including some versions of Ultrix, use of profiling
7210d565efSmrgcauses static variable destructors (currently used only in C++) not to
7310d565efSmrgbe run.
7410d565efSmrg
7510d565efSmrg@item
7610d565efSmrgOn a SPARC, GCC aligns all values of type @code{double} on an 8-byte
7710d565efSmrgboundary, and it expects every @code{double} to be so aligned.  The Sun
7810d565efSmrgcompiler usually gives @code{double} values 8-byte alignment, with one
7910d565efSmrgexception: function arguments of type @code{double} may not be aligned.
8010d565efSmrg
8110d565efSmrgAs a result, if a function compiled with Sun CC takes the address of an
8210d565efSmrgargument of type @code{double} and passes this pointer of type
8310d565efSmrg@code{double *} to a function compiled with GCC, dereferencing the
8410d565efSmrgpointer may cause a fatal signal.
8510d565efSmrg
8610d565efSmrgOne way to solve this problem is to compile your entire program with GCC@.
8710d565efSmrgAnother solution is to modify the function that is compiled with
8810d565efSmrgSun CC to copy the argument into a local variable; local variables
8910d565efSmrgare always properly aligned.  A third solution is to modify the function
9010d565efSmrgthat uses the pointer to dereference it via the following function
9110d565efSmrg@code{access_double} instead of directly with @samp{*}:
9210d565efSmrg
9310d565efSmrg@smallexample
9410d565efSmrginline double
9510d565efSmrgaccess_double (double *unaligned_ptr)
9610d565efSmrg@{
9710d565efSmrg  union d2i @{ double d; int i[2]; @};
9810d565efSmrg
9910d565efSmrg  union d2i *p = (union d2i *) unaligned_ptr;
10010d565efSmrg  union d2i u;
10110d565efSmrg
10210d565efSmrg  u.i[0] = p->i[0];
10310d565efSmrg  u.i[1] = p->i[1];
10410d565efSmrg
10510d565efSmrg  return u.d;
10610d565efSmrg@}
10710d565efSmrg@end smallexample
10810d565efSmrg
10910d565efSmrg@noindent
11010d565efSmrgStoring into the pointer can be done likewise with the same union.
11110d565efSmrg
11210d565efSmrg@item
11310d565efSmrgOn Solaris, the @code{malloc} function in the @file{libmalloc.a} library
11410d565efSmrgmay allocate memory that is only 4 byte aligned.  Since GCC on the
11510d565efSmrgSPARC assumes that doubles are 8 byte aligned, this may result in a
11610d565efSmrgfatal signal if doubles are stored in memory allocated by the
11710d565efSmrg@file{libmalloc.a} library.
11810d565efSmrg
11910d565efSmrgThe solution is to not use the @file{libmalloc.a} library.  Use instead
12010d565efSmrg@code{malloc} and related functions from @file{libc.a}; they do not have
12110d565efSmrgthis problem.
12210d565efSmrg
12310d565efSmrg@item
12410d565efSmrgOn the HP PA machine, ADB sometimes fails to work on functions compiled
12510d565efSmrgwith GCC@.  Specifically, it fails to work on functions that use
12610d565efSmrg@code{alloca} or variable-size arrays.  This is because GCC doesn't
12710d565efSmrggenerate HP-UX unwind descriptors for such functions.  It may even be
12810d565efSmrgimpossible to generate them.
12910d565efSmrg
13010d565efSmrg@item
13110d565efSmrgDebugging (@option{-g}) is not supported on the HP PA machine, unless you use
13210d565efSmrgthe preliminary GNU tools.
13310d565efSmrg
13410d565efSmrg@item
13510d565efSmrgTaking the address of a label may generate errors from the HP-UX
13610d565efSmrgPA assembler.  GAS for the PA does not have this problem.
13710d565efSmrg
13810d565efSmrg@item
13910d565efSmrgUsing floating point parameters for indirect calls to static functions
14010d565efSmrgwill not work when using the HP assembler.  There simply is no way for GCC
14110d565efSmrgto specify what registers hold arguments for static functions when using
14210d565efSmrgthe HP assembler.  GAS for the PA does not have this problem.
14310d565efSmrg
14410d565efSmrg@item
14510d565efSmrgIn extremely rare cases involving some very large functions you may
14610d565efSmrgreceive errors from the HP linker complaining about an out of bounds
14710d565efSmrgunconditional branch offset.  This used to occur more often in previous
14810d565efSmrgversions of GCC, but is now exceptionally rare.  If you should run
14910d565efSmrginto it, you can work around by making your function smaller.
15010d565efSmrg
15110d565efSmrg@item
15210d565efSmrgGCC compiled code sometimes emits warnings from the HP-UX assembler of
15310d565efSmrgthe form:
15410d565efSmrg
15510d565efSmrg@smallexample
15610d565efSmrg(warning) Use of GR3 when
15710d565efSmrg  frame >= 8192 may cause conflict.
15810d565efSmrg@end smallexample
15910d565efSmrg
16010d565efSmrgThese warnings are harmless and can be safely ignored.
16110d565efSmrg
16210d565efSmrg@item
16310d565efSmrgIn extremely rare cases involving some very large functions you may
16410d565efSmrgreceive errors from the AIX Assembler complaining about a displacement
16510d565efSmrgthat is too large.  If you should run into it, you can work around by
16610d565efSmrgmaking your function smaller.
16710d565efSmrg
16810d565efSmrg@item
16910d565efSmrgThe @file{libstdc++.a} library in GCC relies on the SVR4 dynamic
17010d565efSmrglinker semantics which merges global symbols between libraries and
17110d565efSmrgapplications, especially necessary for C++ streams functionality.
17210d565efSmrgThis is not the default behavior of AIX shared libraries and dynamic
17310d565efSmrglinking.  @file{libstdc++.a} is built on AIX with ``runtime-linking''
17410d565efSmrgenabled so that symbol merging can occur.  To utilize this feature,
17510d565efSmrgthe application linked with @file{libstdc++.a} must include the
17610d565efSmrg@option{-Wl,-brtl} flag on the link line.  G++ cannot impose this
17710d565efSmrgbecause this option may interfere with the semantics of the user
17810d565efSmrgprogram and users may not always use @samp{g++} to link his or her
17910d565efSmrgapplication.  Applications are not required to use the
18010d565efSmrg@option{-Wl,-brtl} flag on the link line---the rest of the
18110d565efSmrg@file{libstdc++.a} library which is not dependent on the symbol
18210d565efSmrgmerging semantics will continue to function correctly.
18310d565efSmrg
18410d565efSmrg@item
18510d565efSmrgAn application can interpose its own definition of functions for
18610d565efSmrgfunctions invoked by @file{libstdc++.a} with ``runtime-linking''
18710d565efSmrgenabled on AIX@.  To accomplish this the application must be linked
18810d565efSmrgwith ``runtime-linking'' option and the functions explicitly must be
18910d565efSmrgexported by the application (@option{-Wl,-brtl,-bE:exportfile}).
19010d565efSmrg
19110d565efSmrg@item
19210d565efSmrgAIX on the RS/6000 provides support (NLS) for environments outside of
19310d565efSmrgthe United States.  Compilers and assemblers use NLS to support
19410d565efSmrglocale-specific representations of various objects including
19510d565efSmrgfloating-point numbers (@samp{.} vs @samp{,} for separating decimal
19610d565efSmrgfractions).  There have been problems reported where the library linked
19710d565efSmrgwith GCC does not produce the same floating-point formats that the
19810d565efSmrgassembler accepts.  If you have this problem, set the @env{LANG}
19910d565efSmrgenvironment variable to @samp{C} or @samp{En_US}.
20010d565efSmrg
20110d565efSmrg@item
20210d565efSmrg@opindex fdollars-in-identifiers
20310d565efSmrgEven if you specify @option{-fdollars-in-identifiers},
20410d565efSmrgyou cannot successfully use @samp{$} in identifiers on the RS/6000 due
20510d565efSmrgto a restriction in the IBM assembler.  GAS supports these
20610d565efSmrgidentifiers.
20710d565efSmrg
20810d565efSmrg@end itemize
20910d565efSmrg
21010d565efSmrg@node Incompatibilities
21110d565efSmrg@section Incompatibilities of GCC
21210d565efSmrg@cindex incompatibilities of GCC
21310d565efSmrg@opindex traditional
21410d565efSmrg
21510d565efSmrgThere are several noteworthy incompatibilities between GNU C and K&R
21610d565efSmrg(non-ISO) versions of C@.
21710d565efSmrg
21810d565efSmrg@itemize @bullet
21910d565efSmrg@cindex string constants
22010d565efSmrg@cindex read-only strings
22110d565efSmrg@cindex shared strings
22210d565efSmrg@item
22310d565efSmrgGCC normally makes string constants read-only.  If several
22410d565efSmrgidentical-looking string constants are used, GCC stores only one
22510d565efSmrgcopy of the string.
22610d565efSmrg
22710d565efSmrg@cindex @code{mktemp}, and constant strings
22810d565efSmrgOne consequence is that you cannot call @code{mktemp} with a string
22910d565efSmrgconstant argument.  The function @code{mktemp} always alters the
23010d565efSmrgstring its argument points to.
23110d565efSmrg
23210d565efSmrg@cindex @code{sscanf}, and constant strings
23310d565efSmrg@cindex @code{fscanf}, and constant strings
23410d565efSmrg@cindex @code{scanf}, and constant strings
23510d565efSmrgAnother consequence is that @code{sscanf} does not work on some very
23610d565efSmrgold systems when passed a string constant as its format control string
23710d565efSmrgor input.  This is because @code{sscanf} incorrectly tries to write
23810d565efSmrginto the string constant.  Likewise @code{fscanf} and @code{scanf}.
23910d565efSmrg
24010d565efSmrgThe solution to these problems is to change the program to use
24110d565efSmrg@code{char}-array variables with initialization strings for these
24210d565efSmrgpurposes instead of string constants.
24310d565efSmrg
24410d565efSmrg@item
24510d565efSmrg@code{-2147483648} is positive.
24610d565efSmrg
24710d565efSmrgThis is because 2147483648 cannot fit in the type @code{int}, so
24810d565efSmrg(following the ISO C rules) its data type is @code{unsigned long int}.
24910d565efSmrgNegating this value yields 2147483648 again.
25010d565efSmrg
25110d565efSmrg@item
25210d565efSmrgGCC does not substitute macro arguments when they appear inside of
25310d565efSmrgstring constants.  For example, the following macro in GCC
25410d565efSmrg
25510d565efSmrg@smallexample
25610d565efSmrg#define foo(a) "a"
25710d565efSmrg@end smallexample
25810d565efSmrg
25910d565efSmrg@noindent
26010d565efSmrgwill produce output @code{"a"} regardless of what the argument @var{a} is.
26110d565efSmrg
26210d565efSmrg@cindex @code{setjmp} incompatibilities
26310d565efSmrg@cindex @code{longjmp} incompatibilities
26410d565efSmrg@item
26510d565efSmrgWhen you use @code{setjmp} and @code{longjmp}, the only automatic
26610d565efSmrgvariables guaranteed to remain valid are those declared
26710d565efSmrg@code{volatile}.  This is a consequence of automatic register
26810d565efSmrgallocation.  Consider this function:
26910d565efSmrg
27010d565efSmrg@smallexample
27110d565efSmrgjmp_buf j;
27210d565efSmrg
27310d565efSmrgfoo ()
27410d565efSmrg@{
27510d565efSmrg  int a, b;
27610d565efSmrg
27710d565efSmrg  a = fun1 ();
27810d565efSmrg  if (setjmp (j))
27910d565efSmrg    return a;
28010d565efSmrg
28110d565efSmrg  a = fun2 ();
28210d565efSmrg  /* @r{@code{longjmp (j)} may occur in @code{fun3}.} */
28310d565efSmrg  return a + fun3 ();
28410d565efSmrg@}
28510d565efSmrg@end smallexample
28610d565efSmrg
28710d565efSmrgHere @code{a} may or may not be restored to its first value when the
28810d565efSmrg@code{longjmp} occurs.  If @code{a} is allocated in a register, then
28910d565efSmrgits first value is restored; otherwise, it keeps the last value stored
29010d565efSmrgin it.
29110d565efSmrg
29210d565efSmrg@opindex W
29310d565efSmrgIf you use the @option{-W} option with the @option{-O} option, you will
29410d565efSmrgget a warning when GCC thinks such a problem might be possible.
29510d565efSmrg
29610d565efSmrg@item
29710d565efSmrgPrograms that use preprocessing directives in the middle of macro
29810d565efSmrgarguments do not work with GCC@.  For example, a program like this
29910d565efSmrgwill not work:
30010d565efSmrg
30110d565efSmrg@smallexample
30210d565efSmrg@group
30310d565efSmrgfoobar (
30410d565efSmrg#define luser
30510d565efSmrg        hack)
30610d565efSmrg@end group
30710d565efSmrg@end smallexample
30810d565efSmrg
30910d565efSmrgISO C does not permit such a construct.
31010d565efSmrg
31110d565efSmrg@item
31210d565efSmrgK&R compilers allow comments to cross over an inclusion boundary
31310d565efSmrg(i.e.@: started in an include file and ended in the including file).
31410d565efSmrg
31510d565efSmrg@cindex external declaration scope
31610d565efSmrg@cindex scope of external declarations
31710d565efSmrg@cindex declaration scope
31810d565efSmrg@item
31910d565efSmrgDeclarations of external variables and functions within a block apply
32010d565efSmrgonly to the block containing the declaration.  In other words, they
32110d565efSmrghave the same scope as any other declaration in the same place.
32210d565efSmrg
32310d565efSmrgIn some other C compilers, an @code{extern} declaration affects all the
32410d565efSmrgrest of the file even if it happens within a block.
32510d565efSmrg
32610d565efSmrg@item
32710d565efSmrgIn traditional C, you can combine @code{long}, etc., with a typedef name,
32810d565efSmrgas shown here:
32910d565efSmrg
33010d565efSmrg@smallexample
33110d565efSmrgtypedef int foo;
33210d565efSmrgtypedef long foo bar;
33310d565efSmrg@end smallexample
33410d565efSmrg
33510d565efSmrgIn ISO C, this is not allowed: @code{long} and other type modifiers
33610d565efSmrgrequire an explicit @code{int}.
33710d565efSmrg
33810d565efSmrg@cindex typedef names as function parameters
33910d565efSmrg@item
34010d565efSmrgPCC allows typedef names to be used as function parameters.
34110d565efSmrg
34210d565efSmrg@item
34310d565efSmrgTraditional C allows the following erroneous pair of declarations to
34410d565efSmrgappear together in a given scope:
34510d565efSmrg
34610d565efSmrg@smallexample
34710d565efSmrgtypedef int foo;
34810d565efSmrgtypedef foo foo;
34910d565efSmrg@end smallexample
35010d565efSmrg
35110d565efSmrg@item
35210d565efSmrgGCC treats all characters of identifiers as significant.  According to
35310d565efSmrgK&R-1 (2.2), ``No more than the first eight characters are significant,
35410d565efSmrgalthough more may be used.''.  Also according to K&R-1 (2.2), ``An
35510d565efSmrgidentifier is a sequence of letters and digits; the first character must
35610d565efSmrgbe a letter.  The underscore _ counts as a letter.'', but GCC also
35710d565efSmrgallows dollar signs in identifiers.
35810d565efSmrg
35910d565efSmrg@cindex whitespace
36010d565efSmrg@item
36110d565efSmrgPCC allows whitespace in the middle of compound assignment operators
36210d565efSmrgsuch as @samp{+=}.  GCC, following the ISO standard, does not
36310d565efSmrgallow this.
36410d565efSmrg
36510d565efSmrg@cindex apostrophes
36610d565efSmrg@cindex @code{'}
36710d565efSmrg@item
36810d565efSmrgGCC complains about unterminated character constants inside of
36910d565efSmrgpreprocessing conditionals that fail.  Some programs have English
37010d565efSmrgcomments enclosed in conditionals that are guaranteed to fail; if these
37110d565efSmrgcomments contain apostrophes, GCC will probably report an error.  For
37210d565efSmrgexample, this code would produce an error:
37310d565efSmrg
37410d565efSmrg@smallexample
37510d565efSmrg#if 0
37610d565efSmrgYou can't expect this to work.
37710d565efSmrg#endif
37810d565efSmrg@end smallexample
37910d565efSmrg
38010d565efSmrgThe best solution to such a problem is to put the text into an actual
38110d565efSmrgC comment delimited by @samp{/*@dots{}*/}.
38210d565efSmrg
38310d565efSmrg@item
38410d565efSmrgMany user programs contain the declaration @samp{long time ();}.  In the
38510d565efSmrgpast, the system header files on many systems did not actually declare
38610d565efSmrg@code{time}, so it did not matter what type your program declared it to
38710d565efSmrgreturn.  But in systems with ISO C headers, @code{time} is declared to
38810d565efSmrgreturn @code{time_t}, and if that is not the same as @code{long}, then
38910d565efSmrg@samp{long time ();} is erroneous.
39010d565efSmrg
39110d565efSmrgThe solution is to change your program to use appropriate system headers
39210d565efSmrg(@code{<time.h>} on systems with ISO C headers) and not to declare
39310d565efSmrg@code{time} if the system header files declare it, or failing that to
39410d565efSmrguse @code{time_t} as the return type of @code{time}.
39510d565efSmrg
39610d565efSmrg@cindex @code{float} as function value type
39710d565efSmrg@item
39810d565efSmrgWhen compiling functions that return @code{float}, PCC converts it to
39910d565efSmrga double.  GCC actually returns a @code{float}.  If you are concerned
40010d565efSmrgwith PCC compatibility, you should declare your functions to return
40110d565efSmrg@code{double}; you might as well say what you mean.
40210d565efSmrg
40310d565efSmrg@cindex structures
40410d565efSmrg@cindex unions
40510d565efSmrg@item
40610d565efSmrgWhen compiling functions that return structures or unions, GCC
40710d565efSmrgoutput code normally uses a method different from that used on most
40810d565efSmrgversions of Unix.  As a result, code compiled with GCC cannot call
40910d565efSmrga structure-returning function compiled with PCC, and vice versa.
41010d565efSmrg
41110d565efSmrgThe method used by GCC is as follows: a structure or union which is
41210d565efSmrg1, 2, 4 or 8 bytes long is returned like a scalar.  A structure or union
41310d565efSmrgwith any other size is stored into an address supplied by the caller
41410d565efSmrg(usually in a special, fixed register, but on some machines it is passed
41510d565efSmrgon the stack).  The target hook @code{TARGET_STRUCT_VALUE_RTX}
41610d565efSmrgtells GCC where to pass this address.
41710d565efSmrg
41810d565efSmrgBy contrast, PCC on most target machines returns structures and unions
41910d565efSmrgof any size by copying the data into an area of static storage, and then
42010d565efSmrgreturning the address of that storage as if it were a pointer value.
42110d565efSmrgThe caller must copy the data from that memory area to the place where
42210d565efSmrgthe value is wanted.  GCC does not use this method because it is
42310d565efSmrgslower and nonreentrant.
42410d565efSmrg
42510d565efSmrgOn some newer machines, PCC uses a reentrant convention for all
42610d565efSmrgstructure and union returning.  GCC on most of these machines uses a
42710d565efSmrgcompatible convention when returning structures and unions in memory,
42810d565efSmrgbut still returns small structures and unions in registers.
42910d565efSmrg
43010d565efSmrg@opindex fpcc-struct-return
43110d565efSmrgYou can tell GCC to use a compatible convention for all structure and
43210d565efSmrgunion returning with the option @option{-fpcc-struct-return}.
43310d565efSmrg
43410d565efSmrg@cindex preprocessing tokens
43510d565efSmrg@cindex preprocessing numbers
43610d565efSmrg@item
43710d565efSmrgGCC complains about program fragments such as @samp{0x74ae-0x4000}
43810d565efSmrgwhich appear to be two hexadecimal constants separated by the minus
43910d565efSmrgoperator.  Actually, this string is a single @dfn{preprocessing token}.
44010d565efSmrgEach such token must correspond to one token in C@.  Since this does not,
44110d565efSmrgGCC prints an error message.  Although it may appear obvious that what
44210d565efSmrgis meant is an operator and two values, the ISO C standard specifically
44310d565efSmrgrequires that this be treated as erroneous.
44410d565efSmrg
44510d565efSmrgA @dfn{preprocessing token} is a @dfn{preprocessing number} if it
44610d565efSmrgbegins with a digit and is followed by letters, underscores, digits,
44710d565efSmrgperiods and @samp{e+}, @samp{e-}, @samp{E+}, @samp{E-}, @samp{p+},
44810d565efSmrg@samp{p-}, @samp{P+}, or @samp{P-} character sequences.  (In strict C90
44910d565efSmrgmode, the sequences @samp{p+}, @samp{p-}, @samp{P+} and @samp{P-} cannot
45010d565efSmrgappear in preprocessing numbers.)
45110d565efSmrg
45210d565efSmrgTo make the above program fragment valid, place whitespace in front of
45310d565efSmrgthe minus sign.  This whitespace will end the preprocessing number.
45410d565efSmrg@end itemize
45510d565efSmrg
45610d565efSmrg@node Fixed Headers
45710d565efSmrg@section Fixed Header Files
45810d565efSmrg
45910d565efSmrgGCC needs to install corrected versions of some system header files.
46010d565efSmrgThis is because most target systems have some header files that won't
46110d565efSmrgwork with GCC unless they are changed.  Some have bugs, some are
46210d565efSmrgincompatible with ISO C, and some depend on special features of other
46310d565efSmrgcompilers.
46410d565efSmrg
46510d565efSmrgInstalling GCC automatically creates and installs the fixed header
46610d565efSmrgfiles, by running a program called @code{fixincludes}.  Normally, you
46710d565efSmrgdon't need to pay attention to this.  But there are cases where it
46810d565efSmrgdoesn't do the right thing automatically.
46910d565efSmrg
47010d565efSmrg@itemize @bullet
47110d565efSmrg@item
47210d565efSmrgIf you update the system's header files, such as by installing a new
47310d565efSmrgsystem version, the fixed header files of GCC are not automatically
47410d565efSmrgupdated.  They can be updated using the @command{mkheaders} script
47510d565efSmrginstalled in
47610d565efSmrg@file{@var{libexecdir}/gcc/@var{target}/@var{version}/install-tools/}.
47710d565efSmrg
47810d565efSmrg@item
47910d565efSmrgOn some systems, header file directories contain
48010d565efSmrgmachine-specific symbolic links in certain places.  This makes it
48110d565efSmrgpossible to share most of the header files among hosts running the
48210d565efSmrgsame version of the system on different machine models.
48310d565efSmrg
48410d565efSmrgThe programs that fix the header files do not understand this special
48510d565efSmrgway of using symbolic links; therefore, the directory of fixed header
48610d565efSmrgfiles is good only for the machine model used to build it.
48710d565efSmrg
48810d565efSmrgIt is possible to make separate sets of fixed header files for the
48910d565efSmrgdifferent machine models, and arrange a structure of symbolic links so
49010d565efSmrgas to use the proper set, but you'll have to do this by hand.
49110d565efSmrg@end itemize
49210d565efSmrg
49310d565efSmrg@node Standard Libraries
49410d565efSmrg@section Standard Libraries
49510d565efSmrg
49610d565efSmrg@opindex Wall
49710d565efSmrgGCC by itself attempts to be a conforming freestanding implementation.
49810d565efSmrg@xref{Standards,,Language Standards Supported by GCC}, for details of
49910d565efSmrgwhat this means.  Beyond the library facilities required of such an
50010d565efSmrgimplementation, the rest of the C library is supplied by the vendor of
50110d565efSmrgthe operating system.  If that C library doesn't conform to the C
50210d565efSmrgstandards, then your programs might get warnings (especially when using
50310d565efSmrg@option{-Wall}) that you don't expect.
50410d565efSmrg
50510d565efSmrgFor example, the @code{sprintf} function on SunOS 4.1.3 returns
50610d565efSmrg@code{char *} while the C standard says that @code{sprintf} returns an
50710d565efSmrg@code{int}.  The @code{fixincludes} program could make the prototype for
50810d565efSmrgthis function match the Standard, but that would be wrong, since the
50910d565efSmrgfunction will still return @code{char *}.
51010d565efSmrg
51110d565efSmrgIf you need a Standard compliant library, then you need to find one, as
51210d565efSmrgGCC does not provide one.  The GNU C library (called @code{glibc})
51310d565efSmrgprovides ISO C, POSIX, BSD, SystemV and X/Open compatibility for
51410d565efSmrgGNU/Linux and HURD-based GNU systems; no recent version of it supports
51510d565efSmrgother systems, though some very old versions did.  Version 2.2 of the
51610d565efSmrgGNU C library includes nearly complete C99 support.  You could also ask
51710d565efSmrgyour operating system vendor if newer libraries are available.
51810d565efSmrg
51910d565efSmrg@node Disappointments
52010d565efSmrg@section Disappointments and Misunderstandings
52110d565efSmrg
52210d565efSmrgThese problems are perhaps regrettable, but we don't know any practical
52310d565efSmrgway around them.
52410d565efSmrg
52510d565efSmrg@itemize @bullet
52610d565efSmrg@item
52710d565efSmrgCertain local variables aren't recognized by debuggers when you compile
52810d565efSmrgwith optimization.
52910d565efSmrg
53010d565efSmrgThis occurs because sometimes GCC optimizes the variable out of
53110d565efSmrgexistence.  There is no way to tell the debugger how to compute the
53210d565efSmrgvalue such a variable ``would have had'', and it is not clear that would
53310d565efSmrgbe desirable anyway.  So GCC simply does not mention the eliminated
53410d565efSmrgvariable when it writes debugging information.
53510d565efSmrg
53610d565efSmrgYou have to expect a certain amount of disagreement between the
53710d565efSmrgexecutable and your source code, when you use optimization.
53810d565efSmrg
53910d565efSmrg@cindex conflicting types
54010d565efSmrg@cindex scope of declaration
54110d565efSmrg@item
54210d565efSmrgUsers often think it is a bug when GCC reports an error for code
54310d565efSmrglike this:
54410d565efSmrg
54510d565efSmrg@smallexample
54610d565efSmrgint foo (struct mumble *);
54710d565efSmrg
54810d565efSmrgstruct mumble @{ @dots{} @};
54910d565efSmrg
55010d565efSmrgint foo (struct mumble *x)
55110d565efSmrg@{ @dots{} @}
55210d565efSmrg@end smallexample
55310d565efSmrg
55410d565efSmrgThis code really is erroneous, because the scope of @code{struct
55510d565efSmrgmumble} in the prototype is limited to the argument list containing it.
55610d565efSmrgIt does not refer to the @code{struct mumble} defined with file scope
55710d565efSmrgimmediately below---they are two unrelated types with similar names in
55810d565efSmrgdifferent scopes.
55910d565efSmrg
56010d565efSmrgBut in the definition of @code{foo}, the file-scope type is used
56110d565efSmrgbecause that is available to be inherited.  Thus, the definition and
56210d565efSmrgthe prototype do not match, and you get an error.
56310d565efSmrg
56410d565efSmrgThis behavior may seem silly, but it's what the ISO standard specifies.
56510d565efSmrgIt is easy enough for you to make your code work by moving the
56610d565efSmrgdefinition of @code{struct mumble} above the prototype.  It's not worth
56710d565efSmrgbeing incompatible with ISO C just to avoid an error for the example
56810d565efSmrgshown above.
56910d565efSmrg
57010d565efSmrg@item
57110d565efSmrgAccesses to bit-fields even in volatile objects works by accessing larger
57210d565efSmrgobjects, such as a byte or a word.  You cannot rely on what size of
57310d565efSmrgobject is accessed in order to read or write the bit-field; it may even
57410d565efSmrgvary for a given bit-field according to the precise usage.
57510d565efSmrg
57610d565efSmrgIf you care about controlling the amount of memory that is accessed, use
57710d565efSmrgvolatile but do not use bit-fields.
57810d565efSmrg
57910d565efSmrg@item
58010d565efSmrgGCC comes with shell scripts to fix certain known problems in system
58110d565efSmrgheader files.  They install corrected copies of various header files in
58210d565efSmrga special directory where only GCC will normally look for them.  The
58310d565efSmrgscripts adapt to various systems by searching all the system header
58410d565efSmrgfiles for the problem cases that we know about.
58510d565efSmrg
58610d565efSmrgIf new system header files are installed, nothing automatically arranges
58710d565efSmrgto update the corrected header files.  They can be updated using the
58810d565efSmrg@command{mkheaders} script installed in
58910d565efSmrg@file{@var{libexecdir}/gcc/@var{target}/@var{version}/install-tools/}.
59010d565efSmrg
59110d565efSmrg@item
59210d565efSmrg@cindex floating point precision
59310d565efSmrgOn 68000 and x86 systems, for instance, you can get paradoxical results
59410d565efSmrgif you test the precise values of floating point numbers.  For example,
59510d565efSmrgyou can find that a floating point value which is not a NaN is not equal
59610d565efSmrgto itself.  This results from the fact that the floating point registers
59710d565efSmrghold a few more bits of precision than fit in a @code{double} in memory.
59810d565efSmrgCompiled code moves values between memory and floating point registers
59910d565efSmrgat its convenience, and moving them into memory truncates them.
60010d565efSmrg
60110d565efSmrg@opindex ffloat-store
60210d565efSmrgYou can partially avoid this problem by using the @option{-ffloat-store}
60310d565efSmrgoption (@pxref{Optimize Options}).
60410d565efSmrg
60510d565efSmrg@item
60610d565efSmrgOn AIX and other platforms without weak symbol support, templates
60710d565efSmrgneed to be instantiated explicitly and symbols for static members
60810d565efSmrgof templates will not be generated.
60910d565efSmrg
61010d565efSmrg@item
61110d565efSmrgOn AIX, GCC scans object files and library archives for static
61210d565efSmrgconstructors and destructors when linking an application before the
61310d565efSmrglinker prunes unreferenced symbols.  This is necessary to prevent the
61410d565efSmrgAIX linker from mistakenly assuming that static constructor or
61510d565efSmrgdestructor are unused and removing them before the scanning can occur.
61610d565efSmrgAll static constructors and destructors found will be referenced even
61710d565efSmrgthough the modules in which they occur may not be used by the program.
61810d565efSmrgThis may lead to both increased executable size and unexpected symbol
61910d565efSmrgreferences.
62010d565efSmrg@end itemize
62110d565efSmrg
62210d565efSmrg@node C++ Misunderstandings
62310d565efSmrg@section Common Misunderstandings with GNU C++
62410d565efSmrg
62510d565efSmrg@cindex misunderstandings in C++
62610d565efSmrg@cindex surprises in C++
62710d565efSmrg@cindex C++ misunderstandings
62810d565efSmrgC++ is a complex language and an evolving one, and its standard
62910d565efSmrgdefinition (the ISO C++ standard) was only recently completed.  As a
63010d565efSmrgresult, your C++ compiler may occasionally surprise you, even when its
63110d565efSmrgbehavior is correct.  This section discusses some areas that frequently
63210d565efSmrggive rise to questions of this sort.
63310d565efSmrg
63410d565efSmrg@menu
63510d565efSmrg* Static Definitions::  Static member declarations are not definitions
63610d565efSmrg* Name lookup::         Name lookup, templates, and accessing members of base classes
63710d565efSmrg* Temporaries::         Temporaries may vanish before you expect
63810d565efSmrg* Copy Assignment::     Copy Assignment operators copy virtual bases twice
63910d565efSmrg@end menu
64010d565efSmrg
64110d565efSmrg@node Static Definitions
64210d565efSmrg@subsection Declare @emph{and} Define Static Members
64310d565efSmrg
64410d565efSmrg@cindex C++ static data, declaring and defining
64510d565efSmrg@cindex static data in C++, declaring and defining
64610d565efSmrg@cindex declaring static data in C++
64710d565efSmrg@cindex defining static data in C++
64810d565efSmrgWhen a class has static data members, it is not enough to @emph{declare}
64910d565efSmrgthe static member; you must also @emph{define} it.  For example:
65010d565efSmrg
65110d565efSmrg@smallexample
65210d565efSmrgclass Foo
65310d565efSmrg@{
65410d565efSmrg  @dots{}
65510d565efSmrg  void method();
65610d565efSmrg  static int bar;
65710d565efSmrg@};
65810d565efSmrg@end smallexample
65910d565efSmrg
66010d565efSmrgThis declaration only establishes that the class @code{Foo} has an
66110d565efSmrg@code{int} named @code{Foo::bar}, and a member function named
66210d565efSmrg@code{Foo::method}.  But you still need to define @emph{both}
66310d565efSmrg@code{method} and @code{bar} elsewhere.  According to the ISO
66410d565efSmrgstandard, you must supply an initializer in one (and only one) source
66510d565efSmrgfile, such as:
66610d565efSmrg
66710d565efSmrg@smallexample
66810d565efSmrgint Foo::bar = 0;
66910d565efSmrg@end smallexample
67010d565efSmrg
67110d565efSmrgOther C++ compilers may not correctly implement the standard behavior.
67210d565efSmrgAs a result, when you switch to @command{g++} from one of these compilers,
67310d565efSmrgyou may discover that a program that appeared to work correctly in fact
67410d565efSmrgdoes not conform to the standard: @command{g++} reports as undefined
67510d565efSmrgsymbols any static data members that lack definitions.
67610d565efSmrg
67710d565efSmrg
67810d565efSmrg@node Name lookup
67910d565efSmrg@subsection Name Lookup, Templates, and Accessing Members of Base Classes
68010d565efSmrg
68110d565efSmrg@cindex base class members
68210d565efSmrg@cindex two-stage name lookup
68310d565efSmrg@cindex dependent name lookup
68410d565efSmrg
68510d565efSmrgThe C++ standard prescribes that all names that are not dependent on
68610d565efSmrgtemplate parameters are bound to their present definitions when parsing
68710d565efSmrga template function or class.@footnote{The C++ standard just uses the
68810d565efSmrgterm ``dependent'' for names that depend on the type or value of
68910d565efSmrgtemplate parameters.  This shorter term will also be used in the rest of
69010d565efSmrgthis section.}  Only names that are dependent are looked up at the point
69110d565efSmrgof instantiation.  For example, consider
69210d565efSmrg
69310d565efSmrg@smallexample
69410d565efSmrg  void foo(double);
69510d565efSmrg
69610d565efSmrg  struct A @{
69710d565efSmrg    template <typename T>
69810d565efSmrg    void f () @{
69910d565efSmrg      foo (1);        // @r{1}
70010d565efSmrg      int i = N;      // @r{2}
70110d565efSmrg      T t;
70210d565efSmrg      t.bar();        // @r{3}
70310d565efSmrg      foo (t);        // @r{4}
70410d565efSmrg    @}
70510d565efSmrg
70610d565efSmrg    static const int N;
70710d565efSmrg  @};
70810d565efSmrg@end smallexample
70910d565efSmrg
71010d565efSmrgHere, the names @code{foo} and @code{N} appear in a context that does
71110d565efSmrgnot depend on the type of @code{T}.  The compiler will thus require that
71210d565efSmrgthey are defined in the context of use in the template, not only before
71310d565efSmrgthe point of instantiation, and will here use @code{::foo(double)} and
71410d565efSmrg@code{A::N}, respectively.  In particular, it will convert the integer
71510d565efSmrgvalue to a @code{double} when passing it to @code{::foo(double)}.
71610d565efSmrg
71710d565efSmrgConversely, @code{bar} and the call to @code{foo} in the fourth marked
71810d565efSmrgline are used in contexts that do depend on the type of @code{T}, so
71910d565efSmrgthey are only looked up at the point of instantiation, and you can
72010d565efSmrgprovide declarations for them after declaring the template, but before
72110d565efSmrginstantiating it.  In particular, if you instantiate @code{A::f<int>},
72210d565efSmrgthe last line will call an overloaded @code{::foo(int)} if one was
72310d565efSmrgprovided, even if after the declaration of @code{struct A}.
72410d565efSmrg
72510d565efSmrgThis distinction between lookup of dependent and non-dependent names is
72610d565efSmrgcalled two-stage (or dependent) name lookup.  G++ implements it
72710d565efSmrgsince version 3.4.
72810d565efSmrg
72910d565efSmrgTwo-stage name lookup sometimes leads to situations with behavior
73010d565efSmrgdifferent from non-template codes.  The most common is probably this:
73110d565efSmrg
73210d565efSmrg@smallexample
73310d565efSmrg  template <typename T> struct Base @{
73410d565efSmrg    int i;
73510d565efSmrg  @};
73610d565efSmrg
73710d565efSmrg  template <typename T> struct Derived : public Base<T> @{
73810d565efSmrg    int get_i() @{ return i; @}
73910d565efSmrg  @};
74010d565efSmrg@end smallexample
74110d565efSmrg
74210d565efSmrgIn @code{get_i()}, @code{i} is not used in a dependent context, so the
74310d565efSmrgcompiler will look for a name declared at the enclosing namespace scope
74410d565efSmrg(which is the global scope here).  It will not look into the base class,
74510d565efSmrgsince that is dependent and you may declare specializations of
74610d565efSmrg@code{Base} even after declaring @code{Derived}, so the compiler cannot
74710d565efSmrgreally know what @code{i} would refer to.  If there is no global
74810d565efSmrgvariable @code{i}, then you will get an error message.
74910d565efSmrg
75010d565efSmrgIn order to make it clear that you want the member of the base class,
75110d565efSmrgyou need to defer lookup until instantiation time, at which the base
75210d565efSmrgclass is known.  For this, you need to access @code{i} in a dependent
75310d565efSmrgcontext, by either using @code{this->i} (remember that @code{this} is of
75410d565efSmrgtype @code{Derived<T>*}, so is obviously dependent), or using
75510d565efSmrg@code{Base<T>::i}.  Alternatively, @code{Base<T>::i} might be brought
75610d565efSmrginto scope by a @code{using}-declaration.
75710d565efSmrg
75810d565efSmrgAnother, similar example involves calling member functions of a base
75910d565efSmrgclass:
76010d565efSmrg
76110d565efSmrg@smallexample
76210d565efSmrg  template <typename T> struct Base @{
76310d565efSmrg      int f();
76410d565efSmrg  @};
76510d565efSmrg
76610d565efSmrg  template <typename T> struct Derived : Base<T> @{
76710d565efSmrg      int g() @{ return f(); @};
76810d565efSmrg  @};
76910d565efSmrg@end smallexample
77010d565efSmrg
77110d565efSmrgAgain, the call to @code{f()} is not dependent on template arguments
77210d565efSmrg(there are no arguments that depend on the type @code{T}, and it is also
77310d565efSmrgnot otherwise specified that the call should be in a dependent context).
77410d565efSmrgThus a global declaration of such a function must be available, since
77510d565efSmrgthe one in the base class is not visible until instantiation time.  The
77610d565efSmrgcompiler will consequently produce the following error message:
77710d565efSmrg
77810d565efSmrg@smallexample
77910d565efSmrg  x.cc: In member function `int Derived<T>::g()':
78010d565efSmrg  x.cc:6: error: there are no arguments to `f' that depend on a template
78110d565efSmrg     parameter, so a declaration of `f' must be available
78210d565efSmrg  x.cc:6: error: (if you use `-fpermissive', G++ will accept your code, but
78310d565efSmrg     allowing the use of an undeclared name is deprecated)
78410d565efSmrg@end smallexample
78510d565efSmrg
78610d565efSmrgTo make the code valid either use @code{this->f()}, or
78710d565efSmrg@code{Base<T>::f()}.  Using the @option{-fpermissive} flag will also let
78810d565efSmrgthe compiler accept the code, by marking all function calls for which no
78910d565efSmrgdeclaration is visible at the time of definition of the template for
79010d565efSmrglater lookup at instantiation time, as if it were a dependent call.
79110d565efSmrgWe do not recommend using @option{-fpermissive} to work around invalid
79210d565efSmrgcode, and it will also only catch cases where functions in base classes
79310d565efSmrgare called, not where variables in base classes are used (as in the
79410d565efSmrgexample above).
79510d565efSmrg
79610d565efSmrgNote that some compilers (including G++ versions prior to 3.4) get these
79710d565efSmrgexamples wrong and accept above code without an error.  Those compilers
79810d565efSmrgdo not implement two-stage name lookup correctly.
79910d565efSmrg
80010d565efSmrg
80110d565efSmrg@node Temporaries
80210d565efSmrg@subsection Temporaries May Vanish Before You Expect
80310d565efSmrg
80410d565efSmrg@cindex temporaries, lifetime of
80510d565efSmrg@cindex portions of temporary objects, pointers to
80610d565efSmrgIt is dangerous to use pointers or references to @emph{portions} of a
80710d565efSmrgtemporary object.  The compiler may very well delete the object before
80810d565efSmrgyou expect it to, leaving a pointer to garbage.  The most common place
80910d565efSmrgwhere this problem crops up is in classes like string classes,
81010d565efSmrgespecially ones that define a conversion function to type @code{char *}
81110d565efSmrgor @code{const char *}---which is one reason why the standard
81210d565efSmrg@code{string} class requires you to call the @code{c_str} member
81310d565efSmrgfunction.  However, any class that returns a pointer to some internal
81410d565efSmrgstructure is potentially subject to this problem.
81510d565efSmrg
81610d565efSmrgFor example, a program may use a function @code{strfunc} that returns
81710d565efSmrg@code{string} objects, and another function @code{charfunc} that
81810d565efSmrgoperates on pointers to @code{char}:
81910d565efSmrg
82010d565efSmrg@smallexample
82110d565efSmrgstring strfunc ();
82210d565efSmrgvoid charfunc (const char *);
82310d565efSmrg
82410d565efSmrgvoid
82510d565efSmrgf ()
82610d565efSmrg@{
82710d565efSmrg  const char *p = strfunc().c_str();
82810d565efSmrg  @dots{}
82910d565efSmrg  charfunc (p);
83010d565efSmrg  @dots{}
83110d565efSmrg  charfunc (p);
83210d565efSmrg@}
83310d565efSmrg@end smallexample
83410d565efSmrg
83510d565efSmrg@noindent
83610d565efSmrgIn this situation, it may seem reasonable to save a pointer to the C
83710d565efSmrgstring returned by the @code{c_str} member function and use that rather
83810d565efSmrgthan call @code{c_str} repeatedly.  However, the temporary string
83910d565efSmrgcreated by the call to @code{strfunc} is destroyed after @code{p} is
84010d565efSmrginitialized, at which point @code{p} is left pointing to freed memory.
84110d565efSmrg
84210d565efSmrgCode like this may run successfully under some other compilers,
84310d565efSmrgparticularly obsolete cfront-based compilers that delete temporaries
84410d565efSmrgalong with normal local variables.  However, the GNU C++ behavior is
84510d565efSmrgstandard-conforming, so if your program depends on late destruction of
84610d565efSmrgtemporaries it is not portable.
84710d565efSmrg
84810d565efSmrgThe safe way to write such code is to give the temporary a name, which
84910d565efSmrgforces it to remain until the end of the scope of the name.  For
85010d565efSmrgexample:
85110d565efSmrg
85210d565efSmrg@smallexample
85310d565efSmrgconst string& tmp = strfunc ();
85410d565efSmrgcharfunc (tmp.c_str ());
85510d565efSmrg@end smallexample
85610d565efSmrg
85710d565efSmrg@node Copy Assignment
85810d565efSmrg@subsection Implicit Copy-Assignment for Virtual Bases
85910d565efSmrg
86010d565efSmrgWhen a base class is virtual, only one subobject of the base class
86110d565efSmrgbelongs to each full object.  Also, the constructors and destructors are
86210d565efSmrginvoked only once, and called from the most-derived class.  However, such
86310d565efSmrgobjects behave unspecified when being assigned.  For example:
86410d565efSmrg
86510d565efSmrg@smallexample
86610d565efSmrgstruct Base@{
86710d565efSmrg  char *name;
868*ec02198aSmrg  Base(const char *n) : name(strdup(n))@{@}
86910d565efSmrg  Base& operator= (const Base& other)@{
87010d565efSmrg   free (name);
87110d565efSmrg   name = strdup (other.name);
872*ec02198aSmrg   return *this;
87310d565efSmrg  @}
87410d565efSmrg@};
87510d565efSmrg
87610d565efSmrgstruct A:virtual Base@{
87710d565efSmrg  int val;
87810d565efSmrg  A():Base("A")@{@}
87910d565efSmrg@};
88010d565efSmrg
88110d565efSmrgstruct B:virtual Base@{
88210d565efSmrg  int bval;
88310d565efSmrg  B():Base("B")@{@}
88410d565efSmrg@};
88510d565efSmrg
88610d565efSmrgstruct Derived:public A, public B@{
88710d565efSmrg  Derived():Base("Derived")@{@}
88810d565efSmrg@};
88910d565efSmrg
89010d565efSmrgvoid func(Derived &d1, Derived &d2)
89110d565efSmrg@{
89210d565efSmrg  d1 = d2;
89310d565efSmrg@}
89410d565efSmrg@end smallexample
89510d565efSmrg
89610d565efSmrgThe C++ standard specifies that @samp{Base::Base} is only called once
89710d565efSmrgwhen constructing or copy-constructing a Derived object.  It is
89810d565efSmrgunspecified whether @samp{Base::operator=} is called more than once when
89910d565efSmrgthe implicit copy-assignment for Derived objects is invoked (as it is
90010d565efSmrginside @samp{func} in the example).
90110d565efSmrg
90210d565efSmrgG++ implements the ``intuitive'' algorithm for copy-assignment: assign all
90310d565efSmrgdirect bases, then assign all members.  In that algorithm, the virtual
90410d565efSmrgbase subobject can be encountered more than once.  In the example, copying
905*ec02198aSmrgproceeds in the following order: @samp{name} (via @code{strdup}),
906*ec02198aSmrg@samp{val}, @samp{name} again, and @samp{bval}.
90710d565efSmrg
90810d565efSmrgIf application code relies on copy-assignment, a user-defined
90910d565efSmrgcopy-assignment operator removes any uncertainties.  With such an
91010d565efSmrgoperator, the application can define whether and how the virtual base
91110d565efSmrgsubobject is assigned.
91210d565efSmrg
91310d565efSmrg@node Non-bugs
91410d565efSmrg@section Certain Changes We Don't Want to Make
91510d565efSmrg
91610d565efSmrgThis section lists changes that people frequently request, but which
91710d565efSmrgwe do not make because we think GCC is better without them.
91810d565efSmrg
91910d565efSmrg@itemize @bullet
92010d565efSmrg@item
92110d565efSmrgChecking the number and type of arguments to a function which has an
92210d565efSmrgold-fashioned definition and no prototype.
92310d565efSmrg
92410d565efSmrgSuch a feature would work only occasionally---only for calls that appear
92510d565efSmrgin the same file as the called function, following the definition.  The
92610d565efSmrgonly way to check all calls reliably is to add a prototype for the
92710d565efSmrgfunction.  But adding a prototype eliminates the motivation for this
92810d565efSmrgfeature.  So the feature is not worthwhile.
92910d565efSmrg
93010d565efSmrg@item
93110d565efSmrgWarning about using an expression whose type is signed as a shift count.
93210d565efSmrg
93310d565efSmrgShift count operands are probably signed more often than unsigned.
93410d565efSmrgWarning about this would cause far more annoyance than good.
93510d565efSmrg
93610d565efSmrg@item
93710d565efSmrgWarning about assigning a signed value to an unsigned variable.
93810d565efSmrg
93910d565efSmrgSuch assignments must be very common; warning about them would cause
94010d565efSmrgmore annoyance than good.
94110d565efSmrg
94210d565efSmrg@item
94310d565efSmrgWarning when a non-void function value is ignored.
94410d565efSmrg
94510d565efSmrgC contains many standard functions that return a value that most
94610d565efSmrgprograms choose to ignore.  One obvious example is @code{printf}.
94710d565efSmrgWarning about this practice only leads the defensive programmer to
94810d565efSmrgclutter programs with dozens of casts to @code{void}.  Such casts are
94910d565efSmrgrequired so frequently that they become visual noise.  Writing those
95010d565efSmrgcasts becomes so automatic that they no longer convey useful
95110d565efSmrginformation about the intentions of the programmer.  For functions
95210d565efSmrgwhere the return value should never be ignored, use the
95310d565efSmrg@code{warn_unused_result} function attribute (@pxref{Function
95410d565efSmrgAttributes}).
95510d565efSmrg
95610d565efSmrg@item
95710d565efSmrg@opindex fshort-enums
95810d565efSmrgMaking @option{-fshort-enums} the default.
95910d565efSmrg
96010d565efSmrgThis would cause storage layout to be incompatible with most other C
96110d565efSmrgcompilers.  And it doesn't seem very important, given that you can get
96210d565efSmrgthe same result in other ways.  The case where it matters most is when
96310d565efSmrgthe enumeration-valued object is inside a structure, and in that case
96410d565efSmrgyou can specify a field width explicitly.
96510d565efSmrg
96610d565efSmrg@item
96710d565efSmrgMaking bit-fields unsigned by default on particular machines where ``the
96810d565efSmrgABI standard'' says to do so.
96910d565efSmrg
97010d565efSmrgThe ISO C standard leaves it up to the implementation whether a bit-field
97110d565efSmrgdeclared plain @code{int} is signed or not.  This in effect creates two
97210d565efSmrgalternative dialects of C@.
97310d565efSmrg
97410d565efSmrg@opindex fsigned-bitfields
97510d565efSmrg@opindex funsigned-bitfields
97610d565efSmrgThe GNU C compiler supports both dialects; you can specify the signed
97710d565efSmrgdialect with @option{-fsigned-bitfields} and the unsigned dialect with
97810d565efSmrg@option{-funsigned-bitfields}.  However, this leaves open the question of
97910d565efSmrgwhich dialect to use by default.
98010d565efSmrg
98110d565efSmrgCurrently, the preferred dialect makes plain bit-fields signed, because
98210d565efSmrgthis is simplest.  Since @code{int} is the same as @code{signed int} in
98310d565efSmrgevery other context, it is cleanest for them to be the same in bit-fields
98410d565efSmrgas well.
98510d565efSmrg
98610d565efSmrgSome computer manufacturers have published Application Binary Interface
98710d565efSmrgstandards which specify that plain bit-fields should be unsigned.  It is
98810d565efSmrga mistake, however, to say anything about this issue in an ABI@.  This is
98910d565efSmrgbecause the handling of plain bit-fields distinguishes two dialects of C@.
99010d565efSmrgBoth dialects are meaningful on every type of machine.  Whether a
99110d565efSmrgparticular object file was compiled using signed bit-fields or unsigned
99210d565efSmrgis of no concern to other object files, even if they access the same
99310d565efSmrgbit-fields in the same data structures.
99410d565efSmrg
99510d565efSmrgA given program is written in one or the other of these two dialects.
99610d565efSmrgThe program stands a chance to work on most any machine if it is
99710d565efSmrgcompiled with the proper dialect.  It is unlikely to work at all if
99810d565efSmrgcompiled with the wrong dialect.
99910d565efSmrg
100010d565efSmrgMany users appreciate the GNU C compiler because it provides an
100110d565efSmrgenvironment that is uniform across machines.  These users would be
100210d565efSmrginconvenienced if the compiler treated plain bit-fields differently on
100310d565efSmrgcertain machines.
100410d565efSmrg
100510d565efSmrgOccasionally users write programs intended only for a particular machine
100610d565efSmrgtype.  On these occasions, the users would benefit if the GNU C compiler
100710d565efSmrgwere to support by default the same dialect as the other compilers on
100810d565efSmrgthat machine.  But such applications are rare.  And users writing a
100910d565efSmrgprogram to run on more than one type of machine cannot possibly benefit
101010d565efSmrgfrom this kind of compatibility.
101110d565efSmrg
101210d565efSmrgThis is why GCC does and will treat plain bit-fields in the same
101310d565efSmrgfashion on all types of machines (by default).
101410d565efSmrg
101510d565efSmrgThere are some arguments for making bit-fields unsigned by default on all
101610d565efSmrgmachines.  If, for example, this becomes a universal de facto standard,
101710d565efSmrgit would make sense for GCC to go along with it.  This is something
101810d565efSmrgto be considered in the future.
101910d565efSmrg
102010d565efSmrg(Of course, users strongly concerned about portability should indicate
102110d565efSmrgexplicitly in each bit-field whether it is signed or not.  In this way,
102210d565efSmrgthey write programs which have the same meaning in both C dialects.)
102310d565efSmrg
102410d565efSmrg@item
102510d565efSmrg@opindex ansi
102610d565efSmrg@opindex std
102710d565efSmrgUndefining @code{__STDC__} when @option{-ansi} is not used.
102810d565efSmrg
102910d565efSmrgCurrently, GCC defines @code{__STDC__} unconditionally.  This provides
103010d565efSmrggood results in practice.
103110d565efSmrg
103210d565efSmrgProgrammers normally use conditionals on @code{__STDC__} to ask whether
103310d565efSmrgit is safe to use certain features of ISO C, such as function
103410d565efSmrgprototypes or ISO token concatenation.  Since plain @command{gcc} supports
103510d565efSmrgall the features of ISO C, the correct answer to these questions is
103610d565efSmrg``yes''.
103710d565efSmrg
103810d565efSmrgSome users try to use @code{__STDC__} to check for the availability of
103910d565efSmrgcertain library facilities.  This is actually incorrect usage in an ISO
104010d565efSmrgC program, because the ISO C standard says that a conforming
104110d565efSmrgfreestanding implementation should define @code{__STDC__} even though it
104210d565efSmrgdoes not have the library facilities.  @samp{gcc -ansi -pedantic} is a
104310d565efSmrgconforming freestanding implementation, and it is therefore required to
104410d565efSmrgdefine @code{__STDC__}, even though it does not come with an ISO C
104510d565efSmrglibrary.
104610d565efSmrg
104710d565efSmrgSometimes people say that defining @code{__STDC__} in a compiler that
104810d565efSmrgdoes not completely conform to the ISO C standard somehow violates the
104910d565efSmrgstandard.  This is illogical.  The standard is a standard for compilers
105010d565efSmrgthat claim to support ISO C, such as @samp{gcc -ansi}---not for other
105110d565efSmrgcompilers such as plain @command{gcc}.  Whatever the ISO C standard says
105210d565efSmrgis relevant to the design of plain @command{gcc} without @option{-ansi} only
105310d565efSmrgfor pragmatic reasons, not as a requirement.
105410d565efSmrg
105510d565efSmrgGCC normally defines @code{__STDC__} to be 1, and in addition
105610d565efSmrgdefines @code{__STRICT_ANSI__} if you specify the @option{-ansi} option,
105710d565efSmrgor a @option{-std} option for strict conformance to some version of ISO C@.
105810d565efSmrgOn some hosts, system include files use a different convention, where
105910d565efSmrg@code{__STDC__} is normally 0, but is 1 if the user specifies strict
106010d565efSmrgconformance to the C Standard.  GCC follows the host convention when
106110d565efSmrgprocessing system include files, but when processing user files it follows
106210d565efSmrgthe usual GNU C convention.
106310d565efSmrg
106410d565efSmrg@item
106510d565efSmrgUndefining @code{__STDC__} in C++.
106610d565efSmrg
106710d565efSmrgPrograms written to compile with C++-to-C translators get the
106810d565efSmrgvalue of @code{__STDC__} that goes with the C compiler that is
106910d565efSmrgsubsequently used.  These programs must test @code{__STDC__}
107010d565efSmrgto determine what kind of C preprocessor that compiler uses:
107110d565efSmrgwhether they should concatenate tokens in the ISO C fashion
107210d565efSmrgor in the traditional fashion.
107310d565efSmrg
107410d565efSmrgThese programs work properly with GNU C++ if @code{__STDC__} is defined.
107510d565efSmrgThey would not work otherwise.
107610d565efSmrg
107710d565efSmrgIn addition, many header files are written to provide prototypes in ISO
107810d565efSmrgC but not in traditional C@.  Many of these header files can work without
107910d565efSmrgchange in C++ provided @code{__STDC__} is defined.  If @code{__STDC__}
108010d565efSmrgis not defined, they will all fail, and will all need to be changed to
108110d565efSmrgtest explicitly for C++ as well.
108210d565efSmrg
108310d565efSmrg@item
108410d565efSmrgDeleting ``empty'' loops.
108510d565efSmrg
108610d565efSmrgHistorically, GCC has not deleted ``empty'' loops under the
108710d565efSmrgassumption that the most likely reason you would put one in a program is
108810d565efSmrgto have a delay, so deleting them will not make real programs run any
108910d565efSmrgfaster.
109010d565efSmrg
109110d565efSmrgHowever, the rationale here is that optimization of a nonempty loop
109210d565efSmrgcannot produce an empty one. This held for carefully written C compiled
109310d565efSmrgwith less powerful optimizers but is not always the case for carefully
109410d565efSmrgwritten C++ or with more powerful optimizers.
109510d565efSmrgThus GCC will remove operations from loops whenever it can determine
109610d565efSmrgthose operations are not externally visible (apart from the time taken
109710d565efSmrgto execute them, of course).  In case the loop can be proved to be finite,
109810d565efSmrgGCC will also remove the loop itself.
109910d565efSmrg
110010d565efSmrgBe aware of this when performing timing tests, for instance the
110110d565efSmrgfollowing loop can be completely removed, provided
110210d565efSmrg@code{some_expression} can provably not change any global state.
110310d565efSmrg
110410d565efSmrg@smallexample
110510d565efSmrg@{
110610d565efSmrg   int sum = 0;
110710d565efSmrg   int ix;
110810d565efSmrg
110910d565efSmrg   for (ix = 0; ix != 10000; ix++)
111010d565efSmrg      sum += some_expression;
111110d565efSmrg@}
111210d565efSmrg@end smallexample
111310d565efSmrg
111410d565efSmrgEven though @code{sum} is accumulated in the loop, no use is made of
111510d565efSmrgthat summation, so the accumulation can be removed.
111610d565efSmrg
111710d565efSmrg@item
111810d565efSmrgMaking side effects happen in the same order as in some other compiler.
111910d565efSmrg
112010d565efSmrg@cindex side effects, order of evaluation
112110d565efSmrg@cindex order of evaluation, side effects
112210d565efSmrgIt is never safe to depend on the order of evaluation of side effects.
112310d565efSmrgFor example, a function call like this may very well behave differently
112410d565efSmrgfrom one compiler to another:
112510d565efSmrg
112610d565efSmrg@smallexample
112710d565efSmrgvoid func (int, int);
112810d565efSmrg
112910d565efSmrgint i = 2;
113010d565efSmrgfunc (i++, i++);
113110d565efSmrg@end smallexample
113210d565efSmrg
113310d565efSmrgThere is no guarantee (in either the C or the C++ standard language
113410d565efSmrgdefinitions) that the increments will be evaluated in any particular
113510d565efSmrgorder.  Either increment might happen first.  @code{func} might get the
113610d565efSmrgarguments @samp{2, 3}, or it might get @samp{3, 2}, or even @samp{2, 2}.
113710d565efSmrg
113810d565efSmrg@item
113910d565efSmrgMaking certain warnings into errors by default.
114010d565efSmrg
114110d565efSmrgSome ISO C testsuites report failure when the compiler does not produce
114210d565efSmrgan error message for a certain program.
114310d565efSmrg
114410d565efSmrg@opindex pedantic-errors
114510d565efSmrgISO C requires a ``diagnostic'' message for certain kinds of invalid
114610d565efSmrgprograms, but a warning is defined by GCC to count as a diagnostic.  If
114710d565efSmrgGCC produces a warning but not an error, that is correct ISO C support.
114810d565efSmrgIf testsuites call this ``failure'', they should be run with the GCC
114910d565efSmrgoption @option{-pedantic-errors}, which will turn these warnings into
115010d565efSmrgerrors.
115110d565efSmrg
115210d565efSmrg@end itemize
115310d565efSmrg
115410d565efSmrg@node Warnings and Errors
115510d565efSmrg@section Warning Messages and Error Messages
115610d565efSmrg
115710d565efSmrg@cindex error messages
115810d565efSmrg@cindex warnings vs errors
115910d565efSmrg@cindex messages, warning and error
116010d565efSmrgThe GNU compiler can produce two kinds of diagnostics: errors and
116110d565efSmrgwarnings.  Each kind has a different purpose:
116210d565efSmrg
116310d565efSmrg@itemize @w{}
116410d565efSmrg@item
116510d565efSmrg@dfn{Errors} report problems that make it impossible to compile your
116610d565efSmrgprogram.  GCC reports errors with the source file name and line
116710d565efSmrgnumber where the problem is apparent.
116810d565efSmrg
116910d565efSmrg@item
117010d565efSmrg@dfn{Warnings} report other unusual conditions in your code that
117110d565efSmrg@emph{may} indicate a problem, although compilation can (and does)
117210d565efSmrgproceed.  Warning messages also report the source file name and line
117310d565efSmrgnumber, but include the text @samp{warning:} to distinguish them
117410d565efSmrgfrom error messages.
117510d565efSmrg@end itemize
117610d565efSmrg
117710d565efSmrgWarnings may indicate danger points where you should check to make sure
117810d565efSmrgthat your program really does what you intend; or the use of obsolete
117910d565efSmrgfeatures; or the use of nonstandard features of GNU C or C++.  Many
118010d565efSmrgwarnings are issued only if you ask for them, with one of the @option{-W}
118110d565efSmrgoptions (for instance, @option{-Wall} requests a variety of useful
118210d565efSmrgwarnings).
118310d565efSmrg
118410d565efSmrg@opindex pedantic
118510d565efSmrg@opindex pedantic-errors
118610d565efSmrgGCC always tries to compile your program if possible; it never
118710d565efSmrggratuitously rejects a program whose meaning is clear merely because
118810d565efSmrg(for instance) it fails to conform to a standard.  In some cases,
118910d565efSmrghowever, the C and C++ standards specify that certain extensions are
119010d565efSmrgforbidden, and a diagnostic @emph{must} be issued by a conforming
119110d565efSmrgcompiler.  The @option{-pedantic} option tells GCC to issue warnings in
119210d565efSmrgsuch cases; @option{-pedantic-errors} says to make them errors instead.
119310d565efSmrgThis does not mean that @emph{all} non-ISO constructs get warnings
119410d565efSmrgor errors.
119510d565efSmrg
119610d565efSmrg@xref{Warning Options,,Options to Request or Suppress Warnings}, for
119710d565efSmrgmore detail on these and related command-line options.
1198