Pointer changes address when exiting function

Asked

Viewed 146 times

6

When performing a dynamic allocation of a vector or matrix in C, the pointer for that allocation changes address when leaving the function, before it was pointing to the initial address of the allocated area and just after the end of the respective function it points to the address 1. As shown below, the pointer is passed as parameter by the function to perform the subsequent manipulation of the allocated data.

#include <stdio.h>
#include <stdlib.h>

void aloca(int *vetorInt, int tamanho);

int main (void) {
    int *vetor;

    aloca(vetor, 2);
    printf("END. NA MAIN: %d", vetor);
}

void aloca(int *vetorInt, int tamanho) {
    //Inicializa o ponteiro com NULL para nao ter problema
    vetorInt = NULL;
    //Aloca n espaços
    vetorInt = (int *) malloc(tamanho * sizeof(int));

    //Verifica se foi alocado e exibe o endereço para qual o ponteiro aponta
    if (vetorInt != NULL) {
        printf("*** VETOR ALOCADO.\nENDERECO NA FUNCAO: %d ***\n", vetorInt);
        getchar();
    } else {
        printf("*** NAO ALOCADO ***\n");
        getchar();
    }
}

When I run the code I check that the address has been changed and at the end I lose access to this vector, not being able to perform memory misalignment or data manipulation. Why does this happen? What is the solution?

2 answers

7


This is a classic problem in C. What happens is that the pointer vetorInt is local to the scope of the function aloca (in the same way as the whole tamanho is also). You modify its value within the function, but the same is lost when leaving it, because the variable vetorInt cease to exist.

There are two ways to solve this:

  • Make the function return the allocated address, and do vetor receive it on main.

  • Change the variable parameter in the function to a pointer pointer: int **vetorInt. Thus it is necessary to pass the address of the pointer on main (&vetor). Within the function, it is also necessary to refer to the variable as *vetorInt. As you can see, this method is a little more confusing, especially for beginners.

  • 1

    The two solutions are actually quite functional, although the second is more complex because it takes into consideration multiple indirect, which many people do not understand well, so the first solution becomes more practical for a more general purpose. Grateful!

4

Ideally it is better to allocate in the place you will use, so it is easier to track what you need to release, but if you want to do it anyway the most appropriate is to return the pointer and not pass as parameter, which complicates the syntax. I took what was unnecessary.

#include <stdio.h>
#include <stdlib.h>

int *aloca(int *vetorInt, int tamanho) {
    vetorInt = malloc(tamanho * sizeof(int));
    if (vetorInt != NULL) printf("*** VETOR ALOCADO.\nENDERECO NA FUNCAO: %d ***\n", vetorInt);
    else printf("*** NAO ALOCADO ***\n");
    return vetorInt;
}

int main (void) {
    int *vetor = aloca(vetor, 2);
    printf("END. NA MAIN: %d", vetor);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Really the ideal is to allocate in the scope where it is used, but the function I am dealing with is much more complex and I will use it several times throughout the software, so I went to a function so I did not need to rewrite it several times. Even so, thank you!

  • Alias C has a function called aloca() is asking for trouble :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.