c++ - Establishing shared memory between different proccesses with limited resources? -


after painful debugging, realized trying use mmap store class in shared memory horrendously stupid.

so, have class 5 variables (strings , ints) accessible running processes. how go coding if not use non standard libraries boost?

one idea comes mind, , it's pretty bad, make variables global , use mmap few times. @ first, tried variables declared in class , mmap'd in constructor, understanding not work either.

i add i'm forced use version not support map_anonymous , map_anon.

hold phone. i, in morning haze, have forgotten tested object mmap'ing , managed update member of object 2 different processes in shared manner. believe problem lies entirely in construct initialization , way declare object. yet similar access in current code causes process abruptly end...

this code:

hangman * h; // i've tried , without using new here              // , talk of placement new may show illumination h =(hangman*) mmap(null, sizeof *h, prot_read | prot_write,                     map_shared | map_anon, -1, 0); 

and here object:

struct hangman{          string word;         string wcopy;         int strikes, wlen;         bool solved;         int test;          hangman(){                 word = getword();                 wlen  = word.length();                 wcopy = word;                 cout<<"wcopy in construc is: " <<wcopy<<endl;                 for(int = 0; < wlen; i++)                         wcopy[i] = '_';                 strikes = 0;                 cout << wcopy <<endl;                 solved = false;                 test = 0;         }    //(...)  }; 

... , fact recall int access fine , string access run amok leads me believe should not use string here. if case, should use c style string?

your problem c++ types clever fit in shared memory, including std::string, not speaking of unique_ptr or containers.

internally, string contain pointer. , hell here : if put string in shared memory, default, point dynamic memory of 1 single process.

imho, 1 use case remembering old c types can used c++. , fixed size char array , not bad, if can accept constraint of maximum size words.

i use :

struct hangman{      char word[maxsize];     char wcopy[maxsize];     int strikes, wlen;     bool solved;     int test;      void init(string orig_word) {          wlen = orig_word.length();         if (wlen >= maxsize) {             // error word long         }         strncpy(word, orig, wlen);         word[wlen] = `\0`;         (int i=0; i<wlen; i++) wcopy[i] = '_';         wcopy[wlen] = `\0`;          cout<<"wcopy in init is: " << word <<endl;         strikes = 0;         cout << wcopy <<endl;         solved = false;         test = 0;     } } 

that way, can sure elements used struct reside in shared memory. , initialize struct once allocated member function init.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -