C: Modify global array inside threads -
C: Modify global array inside threads -
i'm making programme creates binary tree of threads have acces unique array of integers. array made in main body , modify threads within function.
the struct of arguments threads :
typedef struct data{ int *array; //pointer global array int others; }data in main body have like:
main(){ int a[18]= {1,3,4,5,6,8,6,5,4,3,5,6,2,5,7,4,5,8}; struct info *data = malloc(sizeof(data)); data->array = a; function(data); } the function, recursive, generates 2 threads , want re-create adress of array in childs
function((void*)firstdata){ pthread_t leftson; pthread_t rightson; struct info *data = firstdata; struct info *dataleft = malloc(sizeof(data)); struct info *datarigh = malloc(sizeof(data)); dataleft->array = data->array dataright->array = info -> array; int i; printf( "\n"); (i = 0; < 18; i++){ printf( " %d ",*(dataleft->array+i)); } printf( "\n"); //print array through boy pthread_create(&leftson,null,function,(void *)dataleft); pthread_create(&righttson,null,function,(void *)dataright); } this errors because of pointer, if seek print using left boy segmentation fault without printing anything, if seek right son, prints array , error, , if don't print @ all, function runs till ens without errors.
i need help understand how manipulate array.
i have tried using malloc()when creating array without results.
note: code demostration,if tried run, lead infinite loop.
c arrays multithreading pointers binary-tree
Comments
Post a Comment