How to define the size of the vector dynamically in C?

4

In C language I can define a vector by specifying the size for example int vet[3]; how can I set the size of the vector dynamic, for example asking the user to inform the size of the vector ?

    
asked by anonymous 22.08.2015 / 03:58

2 answers

3

Basically you have two forms:

  • Use malloc() and friends
  • Use of VLA , from C99
  • Using malloc() and friends you can change the size of the array whenever you need; using VLA the size is fixed once defined.

    Using VLA the array is actually an array whereas with malloc() and friends what you have is a pointer to a memory area.

    Example with malloc() and friends

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        int *pa = NULL;
        size_t sa = 0;
        for (;;) {
            printf("Enter size: ");
            fflush(stdout);
            if (scanf("%zu", &sa) != 1) break;
            pa = malloc(sa * sizeof *pa);
            if (pa == NULL) {
                fprintf(stderr, "erro na alocacao\n");
                exit(EXIT_FAILURE);
            }
            // usa pa
            // desde pa[0] ate pa[sa - 1]
            free(pa);
        }
    }
    

    Example with VLA

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        size_t sa = 0;
        for (;;) {
            printf("Enter size: ");
            fflush(stdout);
            if (scanf("%zu", &sa) != 1) break;
            int arr[sa];
            // usa arr
            // desde arr[0] ate arr[sa - 1]
        }
    }
    
        
    22.08.2015 / 10:47
    4

    To allocate a vector dynamically, you must use the function malloc or calloc . You should know how to operate pointers to briefly understand how it works.

    For example, to allocate a vector with 10 integer positions:

    int *vetor = malloc(sizeof(int) * 10);

    The same thing can be done with the function calloc like this:

    int *vetor = calloc (10,sizeof(int))

    You can change the value 10 to some variable you have read from the user before.

    Accessing the vector positions is done in the same way as if it were a statically allocated vector: vetor[0] , vetor[1] , and so on.

    If you need to change the size of the vector at runtime (dynamically), you can use the realloc function, for example, to increase the size of the vector allocated to 20 :

    vetor = realloc(vetor, 20 * sizeof(int));

    Do not forget to include the <stdlib.h> library that contains the dynamic allocation functions.

    Important: This space will no longer be released unless you explicitly release it with the free(vetor) command. If you forget to deallocate and lose the reference to the vector pointer, your program will have wasted memory that you will no longer be able to recover. This happens for dynamically allocated memory.

    Modern operating systems deallocate the memory of your program (including the one that was lost) after running it, even if you forget to deallocate manually.

        
    22.08.2015 / 07:20