954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Free array of pointers

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!

habib_parisa
Newbie Poster
16 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

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?

habib_parisa
Newbie Poster
16 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 
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!

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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?

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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!

habib_parisa
Newbie Poster
16 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: