Can i return address of a variable in a function of return type pointer?

2

I am trying to understand how pointer to a variable and address of a variable works.

When I am writing the following code :

#include<stdio.h>
int* g(void){
    int x = 10;
    int *p = &x;
    printf("%p,%p",p,&x);
    return (p);
}
void main(){
    int * p = g();
    printf("\n%d",*p);
}

Output:

0060FED8,0060FED8
10
Process returned 3 (0x3)   execution time : 0.031 s

Both the address and pointer to x gives the same value, but when i am returning address &x instead of pointer p the code doesn't print the value of x anymore:

#include<stdio.h>
int* g(void){
    int x = 10;
    int *p = &x;
    printf("%p,%p",p,&x);
    return (&x);
}
void main(){
    int * p = g();
    printf("\n%d",*p);
}

Warning:

warning: function returns address of local variable [-Wreturn-local-addr]

Output:

0060FED8,0060FED8
Process returned -1073741819 (0xC0000005)   execution time : 8.332 s

Can anyone tell me what am I doing wrong?

c
asked on Stack Overflow Mar 29, 2019 by Gunjan

3 Answers

1

What you have been observing is an Undefined Behaviour. There is something called Storage Class in C programming language. The variables inside your function int *g(void) has automatic storage class. They have a lifetime and scope of your function. Whenever you get out of your function, the location allocated for your variables (x and *p) will be destroyed.

So, it doesn't make sense to use address of variables with automatic storage class outside their scope, i.e., outside your function (int *g(void)) as the pointer returned will be a dangling pointer.

You can make use of heap dynamic memory to allocate the space if you want to learn or use memory location outside the function scope.

You can also make use of global variable and return memory address of that global variable from your function.

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

int x = 10;

int *g(void)
{
    int *p = &x;
    printf("%p,%p\r\n", p, &x);
    return (&x);
}

int *heap(void)
{
    int *p = malloc(sizeof(int));
    *p = 100;
    printf("%p\r\n", p);
    return p;
}


int main(void)
{
    int *p = g();

    printf("%d\r\n", *p);

    int *p1 = heap();
    printf("%d\r\n", *p1);

    return 0;
}
answered on Stack Overflow Mar 29, 2019 by abhiarora
0

Since x isn't allocated memory on the heap with malloc or calloc, it's instead allocated on what's called the stack. The stack is used for primarily local variables. A local variable exists only within the scope it's declared. A scope is in poor man's terms, anything between two sets of matching braces.

int* g(void){
    //x is allocated on the stack, since malloc wasn't used
    int x = 10;
    //p is pointing to x's spot in memory (on the stack)
    int *p = &x;
    //These addresses are the same, all is well so far
    printf("%p,%p",p,&x);
    //We return the memory location of x
    //However, since this is the end of this scope (this function), any variables on the
    //stack from this scope will be deleted, which means by the time we get back to main
    //the variable "x" was deleted, so its spot in memory doesn't mean anything
    return (&x);
}
answered on Stack Overflow Mar 29, 2019 by EnigmaticBacon
0

Function g has x declared as local variable. This means that x is only seen/available to function where it is declared. Passing pointer of x to main is useless since x is not visible to main() function. It is always bad practice to return using pointer unless necessary.

answered on Stack Overflow Mar 29, 2019 by Suven Pandey

User contributions licensed under CC BY-SA 3.0