Reputation: 249
As local variables are also called automatic variables, and are supposed to be allocated memory at run time, when function is accessed.
int main(){
int a; // declaration
return 0;
}
int main(){
int a[]; // compilation error, array_size missing
return 0;
}
int main(){
int a[2]; // declaration, but can't work without array_size,
// so at compile time it is checked!
return 0;
}
My question is whether it's just a rule to give array_size in declaration in C, or memory is allocated at compile time for array (still local variable)
How does it work?
An array is a variable as per C programming by K&R. pg no 161.
Upvotes: 11
Views: 32219
Reputation: 8195
There's a difference between local and automatic variables in C. A local variable may be automatic or static, which determines whether the memory for it is allocated on the stack, or permanently, when the program is first executed.
With this code:
int main(){
int a[]; //compilation error, array_size missing
return 0;
}
This is an incomplete array. The error is because the compiler doesn't know how many int
s the program will need to allocate.
Upvotes: 1
Reputation: 5163
When you declare local variable, the size of it is known at a compile time, but memory allocation occurs during execution time.
So in your examples, array without a size is clearly a problem to compiler, as it doesn't know what is the size to include into assembler code.
If you don't know the size of an array, you can always use pointer types and malloc
/free
or even alloca
. The first two operate on heap, and alloca
actually uses stack.
The notable exception is static variables. The storage for them is allocated at a compile/link time already and can't be changed at runtime.
Examples:
int main(int argc, const char *argv[])
{
int a; // a is a sizeof(int) allocated on stack
}
int main(int argc, const char *argv[])
{
int a[2]; // a is a sizeof(int)*2 allocated on stack
}
int main(int argc, const char *argv[])
{
int *a; // a is a sizeof(int*) allocated on stack (pointer)
a = alloca(sizeof(int)*4); // a points to an area with size of 4 integers
// data is allocated on stack
}
int main(int argc, const char *argv[])
{
static int a; // a is allocated in data segment, keeps the value
}
Upvotes: 12
Reputation: 279245
int main(){
int a[2];
return 0;
}
Here, int a[2];
is a definition of a variable named a
. a
is an array of two int
.
What happens in practice is that the compiler emits code to use space on the stack for 2 adjacent int
objects (probably 8 bytes, but the size of int
is up to the implementation). That's assuming of course that the object isn't removed by the optimizer because you never use it.
The compiler error you got for int a[999999999];
is due to some hard limit enforced by the compiler, because it knows (or anyway assumes) there will never be enough stack for that.
Upvotes: 2
Reputation:
For local variables, the memory they consume is on the stack. This means that they must have a fixed size known at compile time, so that when the function is called, the exact amount of memory needed is added to the stack by changing the value of the stack pointer. This is why the array must have a size: the number of bytes on the stack must change by a fixed amount when the function is called.
Calls to malloc() and similar allocate memory from the heap; memory allocated in this way at runtime can be of variable size.
Upvotes: -1
Reputation: 244
Thats the point in C, where arrays and pointers aren't the same.
Take this example:
int main(){
int a[5];
int * b = malloc(sizeof(int) * 5);
printf("sizeof a = %d\n",sizeof a);
printf("sizeof int[5] = %d\n",sizeof(int[5]));
printf("sizeof b = %d\n",sizeof b);
free(b);
return 0;
}
This will return:
sizeof a = 20
sizeof int[5] = 20
sizeof b = 4
The variable a is internal declared as int[5], an integer pointer pointing to a memory block with space for 5 integers.
Upvotes: 0
Reputation: 21
Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable. The space for an automatic variable is allocated when the compound statement containing the declaration is entered, and is freed when that compound statement is exited.
Upvotes: 0
Reputation: 20794
As sgar91 stated in the comments, an array such as in your example is allocated when a function is called and must be determinate in size. If you need dynamic arrays, you must allocate the memory on the heap.
int *values = malloc(sizeof(int) * array_count);
Upvotes: 0