I am trying to dynamically allocate a array of pthread pointers, but get this glibc error:
*** glibc detected *** ./test: realloc(): invalid next size: 0x00000000084d2010 ***
The code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main(int argc, char** argv) {
pthread_t **threads;
int i;
for(i=1;i<100;i++) {
threads = realloc(threads, sizeof(pthread_t*)*i);
threads[i] = malloc(sizeof(pthread_t));
}
return EXIT_SUCCESS;
}
What am i doing wrong here?
threads(emphasis is mine).sizeof(pthread_t *) * idoesn’t allocate enough memory to access tothread[i]. You have to allocate((sizeof(pthread_t *) * (i + 1)).