Leem.fin Leem.fin - 2 months ago 11
C Question

pass by reference but the value of parameter is null

I am programming in C.

I have a struct:

struct MY_TYPE {
boolean flag;
short int value;
double stuff;
};


I have a function which takes a pointer to pointer of
MY_TYPE
as parameter:

getData(struct MY_TYPE ** m_type) {
// I initialise an object of MY_TYPE
struct MY_TYPE a = {.value = 123};
// I assign the address of above object to a pointer of MY_TYPE
struct MY_TYPE *p = &a;
// I assign the address of above pointer to parameter
m_type = &p;
}


In my main program, I call the above function:

struct MY_TYPE *my_param;
getData(&my_param);

// I set a break pointer here, and it shows me my_param is NULL, why?


After I called getData(...), the parameter I passed in is NULL, why?

Answer

This is a would-be undefined behavior, which does not happen because you are assigning a pointer passed by value.

  • Any changes that you make to m_type inside getData are ignored by the caller. You need to assign *m_type in order for the change to make any difference.
  • With this change in place, you would start getting undefined behavior, because struct a goes out of scope as soon as getData returns.

You can fix this by returning a dynamically allocated block initialized inside your function:

getData(struct MY_TYPE ** m_type) {
  // I initialize an object of MY_TYPE
  struct MY_TYPE a = {.value = 123};
  // I make a copy into dynamic memory
  struct MY_TYPE *copy = malloc(sizeof(struct MY_TYPE));
  memcpy(copy, &a);
  // I assign the address of above pointer to parameter
  *m_type = copy;
}

The caller needs to free the memory received from the call:

struct MY_TYPE *my_param;
getData(&my_param);
... // Use my_param here.
// Now that I am done with my_param...
free(my_param);