Pointer return that points to a local character vector?

1
#include <stdio.h>

char *local(void);

void main(void) {
    char *ponteiro = local();
    printf("%s\n", ponteiro); /*Saida = - (*/
}

char *local(void) {
    char nome[12] = "programador";
    char *ponteiro = nome;
    return ponteiro;
}

In the example above I have the following comment commented out in the code "- (" I think it's some garbage in the memory region, but if I access the pointer by means of index it prints me the character, for example ponteiro[0] "p", now if I use the class type modifier of storage static in variable nome the output occurs as expected.

Why is this happening on the three occasions mentioned?

    
asked by anonymous 09.05.2018 / 05:29

1 answer

1

Let's understand what each line is doing:

char nome[12] = "programador";

Created a space in the data stack for a pointer to characters that will be allocated in a static area of the application . There are 12 characters (11 useful because of the terminator). This pointer is called nome .

char *ponteiro = nome;

You created a space in the data stack for a pointer (4 or 8 bytes depending on the architecture) and put the pointer to the previous space referenced as nome .

return ponteiro;

It terminated the execution of the function by returning (copying) the value of that pointer, "destroying" any area allocated to that function (in fact it is only unavailable)

Now we return to the main function. ponteiro here has the same value as' function call pointer, there was a copy.

printf("%s\n", ponteiro); /*Saida = - (*/

The pointer there is pointing and porting to the memory area that has been destroyed. Anything can happen. This is called undefined behavior .

When you put a static in the variable you are saying that you want to allocate the content in the static area of your application, an area that is never destroyed (and different from stack ), then the data is there as you wish.

This code best demonstrates what you want:

#include <stdio.h>
#include <string.h>

char *local(void) {
    static char nomex[12] = "aaaaaaaaaa";
    char nome[12] = "programador";
    char *ponteiro = nome;
    printf("%s\n", nome);
    printf("%s\n", ponteiro);
    printf("%p\n", (void *)ponteiro);
    printf("%p\n", (void *)nome);
    printf("%p\n", (void *)nomex); 
    printf("%p\n", (void *)&ponteiro); 
    return ponteiro;
}

int main(void) {
    char *ponteiro = local();
    printf("%s\n", ponteiro);
    printf("%c\n", ponteiro[0]);
    printf("%p\n", (void *)ponteiro);
}

Now look at this:

#include <stdio.h>
#include <string.h>

char *local(void) {
    static char nomex[12] = "aaaaaaaaaa";
    char nome[12] = "programador";
    char *ponteiro = nome;
    return ponteiro;
}

int main(void) {
    char *ponteiro = local();
    printf("%s\n", ponteiro);
    printf("%c\n", ponteiro[0]);
    printf("%p\n", (void *)ponteiro);
}

Depending on the compiler it will give different results, even if you do not change anything in the variables.

    
09.05.2018 / 06:48