'Segmentation fault' with pthreads and arrary of pointers -
ok, i've hit wall program. using pthreads implement parallel program, have come "segmentation fault" block.
//this in main function //{{{{{ //creates array holds pointers bodies body **bodies = new body*[number_of_bodies]; int bodiesperthread = number_of_bodies / number_of_threads; //creates array hold pthreads pthread_t threads[number_of_threads]; //here partition array of bodies each thread for(int j = 0; j < number_of_threads; j++) { if(j == (number_of_threads - 1)) { //create new array hold bodies body **subbodies = new body*[bodiesremaining]; // insert bodies new sub array. for(int = nextindex; < bodiesremaining; i++) subbodies[i] = bodies[i]; // launch thread given sub array. pthread_create(&threads[j], null, &tree::updatehelp, (void*)subbodies); } else { // if not last thread create sub array length equal // bodies per thread. update bodies remaining. body **subbodies = new body*[bodiesperthread]; bodiesremaining -= bodiesperthread; // populate new array. for(int = nextindex; < bodiesperthread; i++) subbodies[i] = bodies[i]; nextindex += bodiesperthread + 1; // launch thread given sub array. pthread_create(&threads[j], null, &tree::updatehelp, (void*)subbodies); } } for(int = 0; < number_of_threads; i++) pthread_join(threads[i], null); //}}}} //this in tree.cpp void* tree::updatehelp(void *bodies) { long tid = pthread_self(); body **subbodies = (body**)bodies; //according codeblocks debugger fault occuring here cout << "thread " << tid << " has body @ (" << subbodies[0]->x << "," << subbodies[0]->y << ") mass = " << subbodies[0]->mass << ".\n"; pthread_exit(null); }
i pretty green c++ , cannot life of me figure out why getting error. because have malloc threads? have of body pointers? should create new bodies sub arrays?
thanks in advance!
Comments
Post a Comment