Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does the free() function affect the value of the first 4 elements of an array?

I’m currently learning about dynamic memory allocation in C, and I’m following a tutorial from https://raw.githubusercontent.com/portfoliocourses/c-example-code/main/dynamicmem.c

I’ve modified the code, like so:

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

int main(void) {
  int size = 0;
  printf("Enter size: ");
  scanf("%d", &size);
  int *a = calloc(size, sizeof(int));
  // initialize the array with some values
  for (int i = 0; i < size; i++) a[i] = size - i;
  // print out the array values
  for (int i = 0; i < size; i++)
    printf("a[%d] = %d\n", i, a[i]);
  printf("\na: %p\n", a);
  
  a = realloc(a, sizeof(int) * (size + 5));
  // set the additional 2 int values in the array to 9
  for (int i = size; i < (size+2); i++) a[i] = 9;

  for (int i = 0; i < (size+2); i++)
    printf("a[%d] = %d\n", i, a[i]);
  printf("\n");

  int *save = a; 
  free(a); 
  printf("save: %p\n", save);
  for (int i = 0; i < (size+2); i++)
    printf("save[%d] = %d\n", i, save[i]);

  return 0;
} 

After inputting 5 into the program, the expected output of the "save" pointer should be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

save[0] = 5
save[1] = 4
save[2] = 3
save[3] = 2
save[4] = 1
save[5] = 9
save[6] = 9

However, it instead outputs

save[0] = -1397267184
save[1] = 576
save[2] = -1397292720
save[3] = 576
save[4] = 1
save[5] = 9
save[6] = 9

The goal of the program is to show how free() makes the memory available again, but not remove the stored data. I’m wondering if it’s because I’m using Windows 10, or because of the GCC compiler? I’ve tried using an online compiler, and a few other Windows 10 systems and they have produced the same result, where save[0] to save[3] outputs arbitrary numbers. Thank you

>Solution :

The goal of the program is to show how free() makes the memory available again, but not remove the stored data.

That is a false statement about free. free releases the reservation for the memory at the address passed to it, and that memory is immediately available for any use, including use by the memory management routines for keeping track of available blocks of memory. They might or might not write into that memory.

Since your program sets save to point to the same place as a, passing a to free means the program may no longer rely on the contents of memory that save points to.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading