By Lundin


2016-09-14 09:20:30 8 Comments

This question is meant to be used as a canonical duplicate for this FAQ:

I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data outside the function, I get crashes or other unexpected program behavior.

Here is a MCVE:

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

void create_array (int* data, int size)
{
  data = malloc(sizeof(*data) * size);
  for(int i=0; i<size; i++)
  {
    data[i] = i;
  }

  print_array(data, size);
}

void print_array (int* data, int size)
{
  for(int i=0; i<size; i++)
  {
    printf("%d ", data[i]);
  }
  printf("\n");
}

int main (void)
{
  int* data;
  const int size = 5;

  create_array(data, size);
  print_array(data, size);  // crash here

  free(data);
}

Whenever print_array is called from inside the create_array function, I get the expected output 0 1 2 3 4, but when I call it from main, I get a program crash.

What is the reason for this?

1 comments

@Lundin 2016-09-14 09:20:30

The reason for this bug is that the data used by the create_array function is a local variable that only exists inside that function. The assigned memory address obtained from malloc is only stored in this local variable and never returned to the caller.


Consider this simple example:

void func (int x)
{
  x = 1;
  printf("%d", x);
}

...
int a;
func(a);
printf("%d", a); // bad, undefined behavior - the program might crash or print garbage

Here, a copy of the variable a is stored locally inside the function, as the parameter x. This is known as pass-by-value.

When x is modified, only that local variable gets changed. The variable a in the caller remains unchanged, and since a is not initialized, it will contain "garbage" and cannot be reliably used.


Pointers are no exception to this pass-by-value rule. In your example, the pointer variable data is passed by value to the function. The data pointer inside the function is a local copy and the assigned address from malloc is never passed back to the caller.

So the pointer variable in the caller remains uninitialized and therefore the program crashes. In addition, the create_array function has also created a memory leak, since after that function execution, there is no longer any pointer in the program keeping track of that chunk of allocated memory.


There are two ways you can modify the function to work as expected. Either by returning a copy of the local variable back to the caller:

int* create_array (int size)
{
  int* data = malloc(sizeof(*data) * size);
  for(int i=0; i<size; i++)
  {
    data[i] = i;
  }

  print_array(data, size);

  return data;
}

int main (void)
{
  int* data;
  const int size = 5;

  data = create_array(size);
  print_array(data, size);
}

or by passing the address to the caller's pointer variable and write directly to the caller variable:

void create_array (int** data, int size)
{
  int* tmp = malloc(sizeof(*tmp) * size);
  for(int i=0; i<size; i++)
  {
    tmp[i] = i;
  }

  *data = tmp;      
  print_array(*data, size);
}

int main (void)
{
  int* data;
  const int size = 5;

  create_array(&data, size);
  print_array(data, size);
}

Either form is fine.

@Nikos 2017-03-17 12:20:31

Very good question and answer. You say that the pointer variable is passed by value, which is interesting, since being a pointer it contains an address. You have made a mistake in your second function, it should be print_array(tmp, size); *data = tmp. Or you can instead write: *data = tmp; printArray(*data, size);

@Lundin 2017-03-17 12:50:16

@RestlessC0bra Thanks, it's been fixed. Though this is a community wiki so you are free to edit it too.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] How do function pointers in C work?

  • 2009-05-08 15:49:17
  • Yuval Adam
  • 767149 View
  • 1158 Score
  • 11 Answer
  • Tags:   c function-pointers

17 Answered Questions

[SOLVED] What REALLY happens when you don't free after malloc?

  • 2009-03-17 15:29:09
  • Scott
  • 104504 View
  • 508 Score
  • 17 Answer
  • Tags:   c malloc free

8 Answered Questions

[SOLVED] C Programming: malloc() inside another function

7 Answered Questions

3 Answered Questions

[SOLVED] Does printf() allocate memory in C?

2 Answered Questions

[SOLVED] Returning dynamic array through void function in C

  • 2013-01-02 11:00:27
  • sotirelisc
  • 1764 View
  • 1 Score
  • 2 Answer
  • Tags:   c malloc return void

2 Answered Questions

[SOLVED] pointer to array of struct

  • 2012-12-20 02:51:45
  • user1832809
  • 405 View
  • 2 Score
  • 2 Answer
  • Tags:   c linux malloc

Sponsored Content