1

I wrote a simple code that takes an integer as input from user and allocates an array of that variable size.

#include <stdio.h>

int main(){
    int n, i;
    scanf("%d", &n);

    int arr[n];
    for(i=0; i<n; i++){
        arr[i] = i;
    }
    for(i=0; i<n; i++){
        printf("%d\t", arr[i]);
    }

    return 0;
}

The above code works perfectly (if the array size is not very large, give seg fault for size around 2,150,000). How is that even allowed? I thought this should be done using dynamic memory allocation since the array size is variable.

My entire concept of memory allocation has been shaken. Please explain under what conditions is this allowed (should that be a legal code).

tabs_over_spaces
  • 352
  • 1
  • 3
  • 14
  • 6
    C has had the concept of a *variable-length array* for nearly 18 years now. Storage is still automatic, but runtime-sized. All the usual restrictions on automatic storage apply. – Kerrek SB Sep 18 '16 at 17:11
  • No warning while compilation as well. compiled as "gcc -Wall test.c -o test" – tabs_over_spaces Sep 18 '16 at 17:14
  • Your question doesn't make sense. Which aspect of your code is "static"?! – Kerrek SB Sep 18 '16 at 17:14
  • When to use malloc then? If I am able to declare just like "int arr[n]"? The way I had learned, you should be using dynamic memory allocation when you do not know the sizes of structures at compile time. What am I missing? – tabs_over_spaces Sep 18 '16 at 17:19
  • Use `malloc` if you need pre-C99 compatibility, or if you need to access memory from outside the current scope (e.g. returning a pointer to an array). – owacoder Sep 18 '16 at 17:21
  • What do you mean, "how do you call it"? You call it by its name, e.g. `arr` in your code. – Kerrek SB Sep 18 '16 at 17:22
  • Oh, I see what you mean. Oh boy, there's so much confusion... first off, there's no such thing as a "dynamic variable". A variable is something with a name, and names are not dynamic. Your `arr` is the same kind of variable as your `n`, so call it whatever you would call `n`. "Block-scope variable with automatic storage" would be the technical term, but "non-static local variable" would pass in polite company, and just "local variable" in a crunch. – Kerrek SB Sep 18 '16 at 17:24
  • Thank you for the answers. I went through [gcc docs](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html) and read through it. Now I am confused, with this extension, should I be using malloc(C)/new(c++), like ever? – tabs_over_spaces Sep 18 '16 at 18:06

1 Answers1

0

Variable-length automatic arrays are allowed starting from ISO C99 version of the standard,

This is quote from 2011 C standard, 6.7.5.2 Array declarators:

If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations with function prototype scope.

If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero. The size of each instance of a variable length array type does not change during its lifetime.

Anatoly Strashkevich
  • 1,834
  • 4
  • 16
  • 32