⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extend.texi

📁 理解和实践操作系统的一本好书
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@samp{uhr} or @samp{UHR} for @code{unsigned short _Fract} and@code{_Sat unsigned short _Fract},@samp{ur} or @samp{UR} for @code{unsigned _Fract} and@code{_Sat unsigned _Fract},@samp{ulr} or @samp{ULR} for @code{unsigned long _Fract} and@code{_Sat unsigned long _Fract},@samp{ullr} or @samp{ULLR} for @code{unsigned long long _Fract}and @code{_Sat unsigned long long _Fract},@samp{hk} or @samp{HK} for @code{short _Accum} and @code{_Sat short _Accum},@samp{k} or @samp{K} for @code{_Accum} and @code{_Sat _Accum},@samp{lk} or @samp{LK} for @code{long _Accum} and @code{_Sat long _Accum},@samp{llk} or @samp{LLK} for @code{long long _Accum} and@code{_Sat long long _Accum},@samp{uhk} or @samp{UHK} for @code{unsigned short _Accum} and@code{_Sat unsigned short _Accum},@samp{uk} or @samp{UK} for @code{unsigned _Accum} and@code{_Sat unsigned _Accum},@samp{ulk} or @samp{ULK} for @code{unsigned long _Accum} and@code{_Sat unsigned long _Accum},and @samp{ullk} or @samp{ULLK} for @code{unsigned long long _Accum}and @code{_Sat unsigned long long _Accum}.GCC support of fixed-point types as specified by the draft technical reportis incomplete:@itemize @bullet@itemPragmas to control overflow and rounding behaviors are not implemented.@end itemizeFixed-point types are supported by the DWARF2 debug information format.@node Zero Length@section Arrays of Length Zero@cindex arrays of length zero@cindex zero-length arrays@cindex length-zero arrays@cindex flexible array membersZero-length arrays are allowed in GNU C@.  They are very useful as thelast element of a structure which is really a header for a variable-lengthobject:@smallexamplestruct line @{  int length;  char contents[0];@};struct line *thisline = (struct line *)  malloc (sizeof (struct line) + this_length);thisline->length = this_length;@end smallexampleIn ISO C90, you would have to give @code{contents} a length of 1, whichmeans either you waste space or complicate the argument to @code{malloc}.In ISO C99, you would use a @dfn{flexible array member}, which isslightly different in syntax and semantics:@itemize @bullet@itemFlexible array members are written as @code{contents[]} withoutthe @code{0}.@itemFlexible array members have incomplete type, and so the @code{sizeof}operator may not be applied.  As a quirk of the original implementationof zero-length arrays, @code{sizeof} evaluates to zero.@itemFlexible array members may only appear as the last member of a@code{struct} that is otherwise non-empty.@itemA structure containing a flexible array member, or a union containingsuch a structure (possibly recursively), may not be a member of astructure or an element of an array.  (However, these uses arepermitted by GCC as extensions.)@end itemizeGCC versions before 3.0 allowed zero-length arrays to be staticallyinitialized, as if they were flexible arrays.  In addition to thosecases that were useful, it also allowed initializations in situationsthat would corrupt later data.  Non-empty initialization of zero-lengtharrays is now treated like any case where there are more initializerelements than the array holds, in that a suitable warning about "excesselements in array" is given, and the excess elements (all of them, inthis case) are ignored.Instead GCC allows static initialization of flexible array members.This is equivalent to defining a new structure containing the originalstructure followed by an array of sufficient size to contain the data.I.e.@: in the following, @code{f1} is constructed as if it were declaredlike @code{f2}.@smallexamplestruct f1 @{  int x; int y[];@} f1 = @{ 1, @{ 2, 3, 4 @} @};struct f2 @{  struct f1 f1; int data[3];@} f2 = @{ @{ 1 @}, @{ 2, 3, 4 @} @};@end smallexample@noindentThe convenience of this extension is that @code{f1} has the desiredtype, eliminating the need to consistently refer to @code{f2.f1}.This has symmetry with normal static arrays, in that an array ofunknown size is also written with @code{[]}.Of course, this extension only makes sense if the extra data comes atthe end of a top-level object, as otherwise we would be overwritingdata at subsequent offsets.  To avoid undue complication and confusionwith initialization of deeply nested arrays, we simply disallow anynon-empty initialization except when the structure is the top-levelobject.  For example:@smallexamplestruct foo @{ int x; int y[]; @};struct bar @{ struct foo z; @};struct foo a = @{ 1, @{ 2, 3, 4 @} @};        // @r{Valid.}struct bar b = @{ @{ 1, @{ 2, 3, 4 @} @} @};    // @r{Invalid.}struct bar c = @{ @{ 1, @{ @} @} @};            // @r{Valid.}struct foo d[1] = @{ @{ 1 @{ 2, 3, 4 @} @} @};  // @r{Invalid.}@end smallexample@node Empty Structures@section Structures With No Members@cindex empty structures@cindex zero-size structuresGCC permits a C structure to have no members:@smallexamplestruct empty @{@};@end smallexampleThe structure will have size zero.  In C++, empty structures are partof the language.  G++ treats empty structures as if they had a singlemember of type @code{char}.@node Variable Length@section Arrays of Variable Length@cindex variable-length arrays@cindex arrays of variable length@cindex VLAsVariable-length automatic arrays are allowed in ISO C99, and as anextension GCC accepts them in C89 mode and in C++.  (However, GCC'simplementation of variable-length arrays does not yet conform in detailto the ISO C99 standard.)  These arrays aredeclared like any other automatic arrays, but with a length that is nota constant expression.  The storage is allocated at the point ofdeclaration and deallocated when the brace-level is exited.  Forexample:@smallexampleFILE *concat_fopen (char *s1, char *s2, char *mode)@{  char str[strlen (s1) + strlen (s2) + 1];  strcpy (str, s1);  strcat (str, s2);  return fopen (str, mode);@}@end smallexample@cindex scope of a variable length array@cindex variable-length array scope@cindex deallocating variable length arraysJumping or breaking out of the scope of the array name deallocates thestorage.  Jumping into the scope is not allowed; you get an errormessage for it.@cindex @code{alloca} vs variable-length arraysYou can use the function @code{alloca} to get an effect much likevariable-length arrays.  The function @code{alloca} is available inmany other C implementations (but not in all).  On the other hand,variable-length arrays are more elegant.There are other differences between these two methods.  Space allocatedwith @code{alloca} exists until the containing @emph{function} returns.The space for a variable-length array is deallocated as soon as the arrayname's scope ends.  (If you use both variable-length arrays and@code{alloca} in the same function, deallocation of a variable-length arraywill also deallocate anything more recently allocated with @code{alloca}.)You can also use variable-length arrays as arguments to functions:@smallexamplestruct entrytester (int len, char data[len][len])@{  /* @r{@dots{}} */@}@end smallexampleThe length of an array is computed once when the storage is allocatedand is remembered for the scope of the array in case you access it with@code{sizeof}.If you want to pass the array first and the length afterward, you canuse a forward declaration in the parameter list---another GNU extension.@smallexamplestruct entrytester (int len; char data[len][len], int len)@{  /* @r{@dots{}} */@}@end smallexample@cindex parameter forward declarationThe @samp{int len} before the semicolon is a @dfn{parameter forwarddeclaration}, and it serves the purpose of making the name @code{len}known when the declaration of @code{data} is parsed.You can write any number of such parameter forward declarations in theparameter list.  They can be separated by commas or semicolons, but thelast one must end with a semicolon, which is followed by the ``real''parameter declarations.  Each forward declaration must match a ``real''declaration in parameter name and data type.  ISO C99 does not supportparameter forward declarations.@node Variadic Macros@section Macros with a Variable Number of Arguments.@cindex variable number of arguments@cindex macro with variable arguments@cindex rest argument (in macro)@cindex variadic macrosIn the ISO C standard of 1999, a macro can be declared to accept avariable number of arguments much as a function can.  The syntax fordefining the macro is similar to that of a function.  Here is anexample:@smallexample#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)@end smallexampleHere @samp{@dots{}} is a @dfn{variable argument}.  In the invocation ofsuch a macro, it represents the zero or more tokens until the closingparenthesis that ends the invocation, including any commas.  This set oftokens replaces the identifier @code{__VA_ARGS__} in the macro bodywherever it appears.  See the CPP manual for more information.GCC has long supported variadic macros, and used a different syntax thatallowed you to give a name to the variable arguments just like any otherargument.  Here is an example:@smallexample#define debug(format, args...) fprintf (stderr, format, args)@end smallexampleThis is in all ways equivalent to the ISO C example above, but arguablymore readable and descriptive.GNU CPP has two further variadic macro extensions, and permits them tobe used with either of the above forms of macro definition.In standard C, you are not allowed to leave the variable argument outentirely; but you are allowed to pass an empty argument.  For example,this invocation is invalid in ISO C, because there is no comma afterthe string:@smallexampledebug ("A message")@end smallexampleGNU CPP permits you to completely omit the variable arguments in thisway.  In the above examples, the compiler would complain, though sincethe expansion of the macro still has the extra comma after the formatstring.To help solve this problem, CPP behaves specially for variable argumentsused with the token paste operator, @samp{##}.  If instead you write@smallexample#define debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)@end smallexampleand if the variable arguments are omitted or empty, the @samp{##}operator causes the preprocessor to remove the comma before it.  If youdo provide some variable arguments in your macro invocation, GNU CPPdoes not complain about the paste operation and instead places thevariable arguments after the comma.  Just like any other pasted macroargument, these arguments are not macro expanded.@node Escaped Newlines@section Slightly Looser Rules for Escaped Newlines@cindex escaped newlines@cindex newlines (escaped)Recently, the preprocessor has relaxed its treatment of escapednewlines.  Previously, the newline had to immediately follow abackslash.  The current implementation allows whitespace in the formof spaces, horizontal and vertical tabs, and form feeds between thebackslash and the subsequent newline.  The preprocessor issues awarning, but treats it as a valid escaped newline and combines the twolines to form a single logical line.  This works within comments andtokens, as well as between tokens.  Comments are @emph{not} treated aswhitespace for the purposes of this relaxation, since they have notyet been replaced with spaces.@node Subscripting@section Non-Lvalue Arrays May Have Subscripts@cindex subscripting@cindex arrays, non-lvalue@cindex subscripting and function valuesIn ISO C99, arrays that are not lvalues still decay to pointers, andmay be subscripted, although they may not be modified or used afterthe next sequence point and the unary @samp{&} operator may not beapplied to them.  As an extension, GCC allows such arrays to besubscripted in C89 mode, though otherwise they do not decay topointers outside C99 mode.  For example,this is valid in GNU C though not valid in C89:@smallexample@groupstruct foo @{int a[4];@};struct foo f();bar (int index)@{  return f().a[index];@}@end group@end smallexample@node Pointer Arith@section Arithmetic on @code{void}- and Function-Pointers@cindex void pointers, arithmetic@cindex void, size of pointer to@cindex function pointers, arithmetic@cindex function, size of pointer toIn GNU C, addition and subtraction operations are supported on pointers to@code{void} and on pointers to functions.  This is done by treating thesize of a @code{void} or of a function as 1.A consequence of this is that @code{sizeof} is also allowed on @code{void}and on function types, and returns 1.@opindex Wpointer-arithThe option @option{-Wpointer-arith} requests a warning if these extensionsare used.@node Initializers@section Non-Constant Initializers@cindex initializers, non-constant@cindex non-constant initializersAs in standard C++ and ISO C99, the elements of an aggregate initializer for anautomatic variable are not required to be constant expressions in GNU C@.Here is an example of an initializer with run-time varying elements:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -