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
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
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