PDA

View Full Version : saving a c string of variable length in a shared memory?



nass
3rd January 2007, 12:24
hi there,
happy new year to everyone!

i'm looking into ways to save a string of variable length in a shared memory segment.
the string will be accessed by 2 applications and should be ASCII extended format.
the one application is a GUI written with QT,in which i write the string length in an int and then a 'char CStringOnShMem' variable follows. in order to write the c string in the memory i memcpy the char* c string to its length , on to a reference &CStringOnShMem in the shared memory. im not so sure this is a nice way but it worked.

on the other applications side (and remembering that the 2 applications have identical shared memory structures) if i try to memcpy the c string in the same way i only get the first 4 bytes of the string copied. if i try to do a realloc() of the &CStringOnShMem i get a segfault at runtime, which is normal considering the compiler knows that &ToChar is 4bytes long and no realloc should change that.

so i went back to the GUI and tried to convert the CStringOnShMem to a char* and do a realloc just b4 the memcpy. but i get a segfault at runtime on the GUI side now too. any suggestions?
thank youi in advance for your help
nass

wysota
3rd January 2007, 15:13
Can we see the code?

nass
3rd January 2007, 15:15
hm in the end i just managed to solve it.
a memcpy is done later on that did not take into account the string length and so never copied its data.
thank you very much for your help
nass

wysota
3rd January 2007, 15:36
Eeeem.... you're welcome :)

nass
3rd January 2007, 15:40
still don't vanish into thin air! im coming up with another question about strings and locales in unix. same subforum different topic:)