Don't worry about initializing auto aggregates with non-constant values.

On UN*X, you can get C99-or-later compilers, and we request that in the
autoconf script, so it's really a requirement.

At least as I read

	https://msdn.microsoft.com/en-us/library/34h23df8%28v=vs.100%29.aspx

Visual Studio 2010 (and earlier, going back to VS .NET 2003) supports
the "Use of block-scope variables initialized with nonconstant
expressions", with an example of an aggregate (array) initialization
involving function calls, so it sounds as if it's available on Windows
with any version of VS that we support.

(If I've missed something, it'll presumably show up when something is
built with MSVC, and we can update this at that point.)

So the only thing to avoid is initializing global or static variables
with a value that has to be evaluated at run time (the ability to do
that is probably present in most environments, as I think C++
constructors for variables with static storage duration might have to be
evaluated before main() is called, but I guess few C compilers bother to
use it).

Expand the example in the hopes of avoiding confusion between "static
storage duration" (which something declared "static" has, but which
anything declared with file scope, whether declared "static" or not,
also has) and "static storage duration and internal linkage", which is
what the "static" keyword specifies.

Change-Id: I338eb0892e656c2ab59519e4bf76e1dfbec2fa7d
Reviewed-on: https://code.wireshark.org/review/22434
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2017-06-27 18:34:30 -07:00
parent 7321df2a45
commit e0a9192ebc
1 changed files with 16 additions and 9 deletions

View File

@ -82,25 +82,32 @@ features. The C99 features that can be used are:
- trailing comma in enum declarations
- inline functions (guaranteed only by use of glib.h)
Don't initialize variables with static storage duration - i.e., global
or static variables - or objects with aggregate type (arrays and
structures) in their declaration with non-constant values. Not all
compilers support this. E.g., if "i" is a static or global variable,
don't declare i as
Don't initialize global or static variables (variables with static
storage duration) in their declaration with non-constant values. Not
all compilers support this. E.g., if "i" is a static or global
variable, don't declare "i" as
guint32 i = somearray[2];
declare it as just
outside a function, or as
static guint32 i = somearray[2];
inside or outside a function, declare it as just
guint32 i;
or
static guint32 i;
and later, in code, initialize it with
i = somearray[2];
instead. Initializations of non-aggregate variables with automatic
storage duration - i.e., local variables - with non-constant values is
permitted, so, within a function
instead. Initializations of variables with automatic storage duration -
i.e., local variables - with non-constant values is permitted, so,
within a function
guint32 i = somearray[2];