Variable Sized Arrays vs calloc in C

37,036

Solution 1

If you declare int array[variable] the memory will be allocated on the stack which is not very good for large, relatively permanent data structures (such as one you might want to return). You don't need to free memory manually if you use the array syntax since it's freed when it goes out of scope. calloc on the other hand will allocate memory dynamically at run time on the heap. You'll have to free it yourself as soon as you're finished with it.

Solution 2

I agree with ocdecio that doesn't allow

int array[variable]

allows some types of variables and expressions to be the array size. But in addition to that, things allocated with malloc and family can be resized using realloc.

Solution 3

Using variable sized arrays on the stack as an auto variable (instead of the heap using calloc/malloc/new/etc) is not a bad idea for a process that is going to run for a long time and will need to make and destroy lots of little arrays. This is because the stack is guaranteed not to ever become fragmented, while memory can and will get fragmented. If you are writing firmware or a service that needs to run for years without stopping, you are almost prohibited from using malloc or new.

Solution 4

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. So, don't foget to set compiler flag -std=c99 or -std=gnu99. Following example will work

#include<stdio.h>

int main()
{
    int n;
    printf("\n\tEnter the number of digits: ");
    scanf("%d", &n);

    char str[n];
    for(int i=0; i < n; i++) {
        scanf("%s", &str[i]);
    }

    printf("\n\tThe entered digits are: %s", str);
return 0;
}

I garantee that :-)

Share:
37,036
nmiller
Author by

nmiller

Interests are programming, philosophy, and science.

Updated on January 21, 2020

Comments

  • nmiller
    nmiller over 4 years

    On the discussion of dynamic memory here: "Intro to C Pointers and Dynamic Memory"

    The author states:

    A memory block like this can effectively be used as a more flexible array. This approach is actually much more common in real-world C programs. It's also more predictable and flexible than a "variable size array"

    The type of memory block he is talking about is as such:

    const int size = 5;
    int * array = calloc(size, sizeof(int));
    

    and then using another pointer to iterate over the array:

    int * index = array;
    for (i = 0; i < size; i++) {
        *index = 1; // or whatever value
        index++;
    }
    

    My question is how is this method better than a standard variable sized array like this?:

    int array[variable];
    

    or dynamic:

    char name[] = "Nick";
    

    The author doesn't really shed much light as to why I should prefer the former method to the latter. Or more specifically: How is it more "predictable and flexible"?

  • nmiller
    nmiller over 15 years
    True, it's not variable length. I guess I should've put "dynamic". As the compiler determines how much should be allocated.
  • Prasanga
    Prasanga over 15 years
    Perhaps -- but "dynamic" still implies run-time to me. With the initialization from a constant, the size of the array is determined at compile-time, not run-time.
  • Jonathan Leffler
    Jonathan Leffler over 15 years
    It is valid in C99 for a local (automatic) variable - it is not valid as an external variable declaration (variable would have to be a compile-time constant).