Reputation: 29
I want to define an Array in c in followed way:
#define MACRO1 5U
#define MACRO2 6U
int arr[MACRO1+MACRO2];
my question is: Is Array arr a dynamical defined Array in Heap or static defined Array in DATA-Segment(or BSS)?
I think, arr is a dynamic defined array. It is similar with an array allocated with malloc function. The reason is, that the mathematical addition will be done firstly in runtime. In Compilertime all macros will be only expanded as followed:
int arr[5U+6U];
I used a Compiler with c90 Standard.
Can anybody help me? Is my argumentation Right?
Upvotes: 1
Views: 825
Reputation: 263217
This:
#define MACRO1 5U
#define MACRO2 6U
int arr[MACRO1+MACRO2];
is equivalent to this:
int arr[5U+6U];
Since 5U+6U
is a constant expression, it's evaluated at compile time, just as if you had written:
int arr[11U];
or, since the particular type of an array length is not part of the array's type:
int arr[11];
Since the length is a constant expression, arr
is not a variable-length array (VLA), and it's legal to define it at file scope. (C90 did not have VLAs. C11 made support for them optional.)
If arr
is defined inside a function without the static
keyword, it has automatic storage duration; in most implementations, this means it's allocated on the "stack". Otherwise, it has static storage duration (it exists for the entire execution of the program, and it's allocated in whatever manner the implementation uses (perhaps a BSS segment or something similar). It will not be allocated in the same manner as by a call to malloc()
(on the "heap").
Constant expressions are defined in section 6.6 of the current C standard (see the N1570 draft).
A constant expression may not contain assignment, decrement, increment, function-call, or comma operators, unless they're in a subexpression that's not evaluated. An integer constant expression may only have operands that are integer constants, enumeration constants, character constants, sizeof
expressions whose results are integer constants, and a handful of other things.
Upvotes: 2
Reputation: 107749
arr
is an array whose size is determined at compile time. In C terminology, it's an array that isn't a variable length array. That's because its size is a constant. The variable can be annotated as static
or put at file scope, and that makes it a global variable, with a size that can be reserved statically (typically included in the BSS, if the C implementation has a BSS).
Any expression that is understood by the preprocessor is a constant. For example, you can have a preprocessor directive
#if MACRO1 + MACRO2 > 4
…
The preprocessor (which is part of the C compiler) performs the computation (looking up the values of MACRO1
and MACRO2
, performing the addition and the comparison) in an early stage of the compilation.
There are also constants that the preprocessor cannot calculate, mostly resulting from sizeof
: the result of sizeof
is a compile-time constant unless the calculation involves the size of a variable length array. So while you cannot write #if sizeof(some_type) > 4
, you can write things like
static unsigned char b[sizeof(x) * 2];
static c = sizeof(x) | 4;
The size of the array b
and the value used to initialize c
are both compile-time constants.
Basically, any expression that doesn't involve the value of a variable or a pointer dereference is a compile-time constant (“constant expression” in the terminology used by the C standard).
On the other hand, the value of a variable (as opposed to a macro) is not a compile-time constant, so it cannot be used where a compile-time constant is required¹.
const x = 3; /* OK */
static c = x; /* not ok in C */
¹ C++ works differently here. My answer is about C.
Upvotes: 0
Reputation: 50110
when u do
#define MACRO1 5U
#define MACRO2 6U
int arr[MACRO1+MACRO2];
the c compiler just sees
int arr[11];
which is probably not 'dynamic' - I quote dynamic because you dont define what 'dynamic' means for you.
Where it gets allocated depends on where the statement appears in the program. Its either on the stack if its in a function or its static if its at file scope
Upvotes: 0