Difficulty in understanding variable-length arrays

2019-04-11 13:41发布

I was reading a book when I found that array size must be given at time of declaration or allocated from heap using malloc at runtime.I wrote this program in C :

#include<stdio.h>

int main() {
  int n, i;
  scanf("%d", &n);
  int a[n];
  for (i=0; i<n; i++) {
    scanf("%d", &a[i]);
  }
  for (i=0; i<n; i++) {
    printf("%d ", a[i]);
  }
  return 0;
}

This code works fine.

My question is how this code can work correctly.Isn't it the violation of basic concept of C that array size must be declared before runtime or allocate it using malloc() at runtime.I'm not doing any of these two things,then why it it working properly ?

Solution to my question is variable length arrays which are supported in C99 but if I play aroundmy code and put the statement int a[n]; above scanf("%d,&n); then it's stops working Why is it so.if variable length arrays are supported in C ?

4条回答
啃猪蹄的小仙女
2楼-- · 2019-04-11 14:16

The C99 standard supports variable length arrays. The length of these arrays is determined at runtime.

查看更多
贪生不怕死
3楼-- · 2019-04-11 14:16

Variable length arrays are a new feature added to C in C99.

"variable length" here means that the size of the array is decided at run-time, not compile time. It does not mean that the size of the array can change after it is created. The array is logically created where it is declared. So your code looks like.

int n, i;

Create two variables n and i. Initially these variables are uninitialised.

scanf("%d", &n);

Read a value into n.

int a[n];

Create an array "a" whose size is the current value of n.

If you swap the second and third steps you try to create an array whose size is determined by an uninitalised value. This is not likely to end well.

The C standard does not specify exactly how the array is stored but in practice most compilers (I belive there are some exceptions) will allocate it on the stack. The normal way to do this is to copy the stack pointer into a "frame pointer" as part of the function preamble. This then allows the function to dynamically modify the stack pointer while keeping track of it's own stack frame.

Variable length arrays are a feature that should be used with caution. Compilers typically do not insert any form of overflow checking on stack allocations. Operating systems typically insert a "gaurd page" after the stack to detect stack overflows and either raise an error or grow the stack, but a sufficiently large array can easilly skip over the guard page.

查看更多
SAY GOODBYE
4楼-- · 2019-04-11 14:32

C will be happy as long as you've declared the array and allocated memory for it before you use it. One of the "features" of C is that it doesn't validate array indices, so it's the responsibility of the programmer to ensure that all memory accesses are valid.

查看更多
5楼-- · 2019-04-11 14:35

Since C99 you can declare variable length arrays at block scope.

Example:

void foo(int n)
{
    int array[n];

    // Initialize the array
    for (int i = 0; i < n; i++) {
        array[i] = 42;
    }
}
查看更多
登录 后发表回答