skmiley1
skmiley1

Reputation: 249

Returning pointer from function and saving it

I printed out the value of e.val in main before and after the call to function, and the value has been changed. I am new to C, so wanted to understand this a little better.

Since I return the new pointer and assign it back to e.val, e.val's value changes.

Questions:

1) Is returning and assigning a new value to e.val basically the same as passing a double pointer to e.val?

#include <stdio.h>
#include <stdlib.h>
typedef struct{

  int * val;

} Event;

//qdsp_classic
int* function(int * ptr);
int* function2();
int main()
{
    Event e;
    int x = 10;
    e.val = &x; 
    printf("orig%p\n", (void *) (e.val));
    e.val = function(e.val);
    printf("New%p\n", (void *) (e.val));
    return 0;
}
int* function(int * ptr) 
{
    ptr = function2();
    printf("%p\n", (void *) ptr);
    return ptr;
}
int* function2() 
    int num = 100;
    int * newPtr = (int *) malloc(sizeof(int));
    return newPtr;
}

Upvotes: 0

Views: 368

Answers (1)

kocica
kocica

Reputation: 6467

If you create instance of struct Event, its allocated on stack and pointer inside has immediate value -> dereferencing would cause undefined behavior. Variable x is on stack aswell and has value 10.

+-------------+
|             |
|    +---+    |                   +----+
|    | * |    |                   | 10 |
|    +---+    |                   +----+
|   int* val  |                     x
+-------------+
    Event e

After assignment e.val = &x; to pointer val is saved address of variable x (In other words val is pointing on x)

+-------------+
|             |
|    +---+    |                   +----+
|    | * |----|------------------>| 10 |
|    +---+    |                   +----+
|   int* val  |                     x
+-------------+
    Event e

int * newPtr = (int *) malloc(sizeof(int)); Creates one int on heap / dynamic storage duration, which is uninitialized and his address (address of first byte) is saved to pointer variable newPtr.

+-------------+                                    HEAP
|             |                                    ----------------
|    +---+    |                   +----+           +---+
|    | * |----|------------------>| 10 |           | ? |
|    +---+    |                   +----+           +---+
|   int* val  |                     x                ^
+-------------+                                      |
    Event e                                          |
                                                     |
     +---+                                           |
     | * |--------------------------------------------
     +---+
  int* newPtr

newPtr is returned and saved to e.val. which means that e.val is no longer pointing on x but since now is pointing on that int on heap.

+-------------+                                    HEAP
|             |                                    ----------------
|    +---+    |                   +----+           +---+
|    | * |----|-------            | 10 |           | ? |
|    +---+    |      |            +----+           +---+
|   int* val  |      |              x                ^
+-------------+      |                               |
    Event e          ---------------------------------

Its also possible to change where does pointer points by passing ** double pointer.

void function(int ** ptr) 
{
    *ptr = function2();
    printf("%p\n", (void *) *ptr);
}

// In main
function(&e.val);

Upvotes: 4

Related Questions