Dear All,

I am trying to use free function in C to free memory of an array of pointers. It does not work and I get segmentation fault. The code looks like this:

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

int main() {
  
  int  *make_pointer(int);
  void read_array(int *array[3]);
  
  /****************/
  
  int *array[3] ;
  
  array[0] = make_pointer(1);
  array[1] = make_pointer(2);
  array[2] = make_pointer(3);
  
  
  /****************/
  
  read_array( array );
  
  /****************/
  /** !! The problem is here !! **/  

  free(array);
  
  
  return 0;
}

/******************************/

int  *make_pointer(int i){

  int *x;
  
  x = malloc(sizeof(int));
  
  *x = i;
  
  return x;

}

/******************************/

void read_array(int *array[3]){
  
  int i;
  
  
  /****************/
  
  for (i=0; i< 3; i++) {
    printf("The value of %d= %d \n", i, *array[i]);
  }
  
  
}

However it works when I write

free(array[0]);
free(array[1]);
free(array[2]);

But this does not look like a clever solution!

Would you have a better idea?

Thanks in advance!

Right away, these lines should occur outside of the main function.

int  *make_pointer(int);
void read_array(int *array[3]);

Also, you should call free the same number of times you call malloc.

This is what I think

When you called malloc, you called it individually on each element in the array of pointers. So extending the same logic, when you call free, you have to call it on each element in the array.
Corrections to this post are invited

This is what I think

When you called malloc, you called it individually on each element in the array of pointers. So extending the same logic, when you call free, you have to call it on each element in the array.
Corrections to this post are invited

But I get then segmentation fault!

Sorry it worked, but is there any more clever way?

free(array)

array was defined with memory for 3 pointers to int in a region of memory that was not the heap, therefore you can not use free() with it.

That would be the equivalent of doing this:

int a;
int *ptr_a;

ptr_a = &a;

free(ptr);

Which for obvious reasons it is a no-no!

your make_pointer function is flawed. which is what i believe Aia is saying, although her example "no-no" should have

line 6: free(ptr_a); to keep the analogy correct


look at "make_pointer" function ... why are you assigning the return pointer to 'i'? what does that mean? what do you hope to accomplish? what does 'i' represent anyhow?

commented: Good :) +8

Damned collapsing quotes

>> your make_pointer function is flawed. which is what i believe Aia is saying, although her example "no-no" should have

line 6: free(ptr_a); to keep the analogy correct

I was trying to recreate a segmentation fault. (joking) ;)
Thank you for the little 'big' correction.

commented: :) +7

your make_pointer function is flawed. which is what i believe Aia is saying, although her example "no-no" should have

line 6: free(ptr_a); to keep the analogy correct


look at "make_pointer" function ... why are you assigning the return pointer to 'i'? what does that mean? what do you hope to accomplish? what does 'i' represent anyhow?

Well this code is a part of a huge code, in the make_pointer function, I create a pointer to an object and return it into the main(). I want to have a very small main() otherwise my main would be a very big part of the code which should not!

no you miss my point.

i'm not saying you shouldn't have a function, or return a pointer. having functions that return pointers is, generally speaking, a good way to structure a program.

what i'm saying is that your function itself is flawed, and the pointer you think you're returning is meaningless.

i could just fix it for you, but i won't.

my questions were written to lead you to think about what you are doing, and why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.