I have allocated heap
memory in the thread function f1
, this storage is used to calculate the value in the heap region so that the main function can see it.
Here is the thread function definition:
void *f1(void *input){
int sum = (int*)malloc(sizeof(int));
/* Do calculation */
pthread_exit((void*)&sum);
}
In the above code, the sum
is the heap allocated storage, whose address is passed as a return value to the sum1
in the main()
.
I join
the thread in the main()
like this:
void *sum1;
pthread_join(tid1,(void**)&sum1);
Once i retrieved the value, i want to free
the allocated memory.
When i use free
in the main, it complains like munmap_chunk(): invalid pointer
How can i explicitly and safely free
this memory ?