I get "corrupted size vs prev_size" error whenever I allocate a dynamic memory in a thread. Whenever I allocate the memory in main() it works fine. But allocating dynamic memory in thread produces the error.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *fib(void *p);
struct mystruct
{
int *array;
int size;
};
int main(int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
struct mystruct obj;
obj.size = atoi(argv[1]);;
pthread_create(&tid, &attr, fib, (void *)&obj);
pthread_join(tid, NULL);
}
void *fib (void *p)
{
struct mystruct *x = (struct mystruct *)p;
x->array = (int *) malloc (x->size);
for (int i=0; i<x->size; i++){
x->array[i] = i;
printf("The array is = %d\n", x->array[i]);
}
}
I've added snapshot for details.
Thanks!