C++ void pointers -


pthread_mutex_t mutexread; int main(int argc, char *argv[]){     pthread_t readerthreads;     pthread_mutex_init(&mutexread, null);     string *fname;     cin>> *fname;     pthread_create(&readerthreads, null, reader_thread, (void*) fname); } void *reader_thread(void *param){     string fname = *(string *) param;     cout<<"filename "<< fname<<endl;     ifstream myfile(fname.c_str());     return null;  } 

the code above throw segmentation fault. messed pointers, not know went wrong , how can fix it?

you declared pointer string, use string , pass address.

pthread_mutex_t mutexread; int main(int argc, char *argv[]){     pthread_t readerthreads;     pthread_mutex_init(&mutexread, null);     string fname;     cin>> fname;     pthread_create(&readerthreads, null, reader_thread, (void*) &fname);     pthread_join(&readerthreads,null); } void *reader_thread(void *param){     string fname = *(string *) param;     cout<<"filename "<< fname<<endl;     ifstream myfile(fname.c_str());     return null;  } 

the other problem don't wait thread termination, stack allocated string may deallocated before thread had time use it... use pthread_join in spawning thread.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -