0
#include <stdio.h>

typedef struct {
  int x,y;
} point;

point* create_point(int x,int y) {
  point p={x,y};
  point* ptr = &p;
  return ptr;
}

int main() {
  point* p1 = create_point(1,2);
  point* p2 = create_point(6,7);
  printf("%d, %d, ", p1->x, p1->y);
  printf("%d, %d \n", p2->x, p2->y);
  return 0;
}

When I compile with repl.it I get 6,7,0,0 as output but when I run the same program with sublime text editor, I get a different output : 6,7,6,7. Does anyone know why? and which output is the correct output? Any help is appreciated, thank you.

  • 2
    Why do you get output? There's no `printf()` anywhere. – Barmar Dec 12 '20 at 02:14
  • `p1 = &p;` is a useless statement, because `p1` is a pointer in the function scope. Refer to "pass-by-value". – vmt Dec 12 '20 at 02:23
  • Please note that & operator is used to get the address of the variable. (* Operator is used as pointer to a variable.). As @Barmar has said, there is no printf in your codes so we cannot say what have encountered, but if you are printing the memory address then of course two different systems will have two different output. – Ken Lee Dec 12 '20 at 02:24
  • my apologies, I updated the code – charmingsalamander Dec 12 '20 at 02:35
  • 1
    Well you are setting (and returning) `ptr` to the address of an automatic variable (`p`), which will go out of scope when the function returns, so this is UB. – vmt Dec 12 '20 at 02:38
  • 1
    Does this answer your question? [How to access a local variable from a different function using pointers?](https://stackoverflow.com/questions/4570366/how-to-access-a-local-variable-from-a-different-function-using-pointers) – Retired Ninja Dec 12 '20 at 02:59
  • 1
    The exam says explicitly: Posting questions online asking for solutions is not allowed – Igor Shinkar Dec 12 '20 at 12:47

1 Answers1

1

Generally, when you have a "factory" function that creates and returns a pointer to an object, the object should be allocated on the heap with malloc(), like so:

int *factory(){
    int *p;
    p = malloc(sizeof(whatever));
    return p;
    }

In C, function returns are by value. In this case, while p is a local, stack-allocated variable, it is a pointer and its value (which is what is passed back to the caller) will be the address of the heap-allocated object returned by malloc() so it is a meaningful value outside the scope of the function.

In your function create_point(), you're returning a pointer, but because p is a local (automatic) variable, it is allocated on the stack and so the pointer to it that you're returning will refer to an address that had been in create_point()'s stack frame. Depending on how a given compiler orders automatic variables on the stack (and what order you access them in) as well as other information it needs to place there per the ABI, it's possible that you might have gotten lucky and received the results you expected if you only called create_point() once, and you would have never detected this error. But the second call's stack frame is likely in the same position as or overlaps with the position of the first call's stack frame on the process's stack, meaning that some or all of the contents left over from the first call (since a function call's stack frame generally isn't cleared once the function returns, the old values will still be there until those memory locations are overwritten) would get clobbered by both the second call to create_point() as well as the subsequent calls to printf().

Kurt Weber
  • 176
  • 10