[interprocess 1.42] Copy multiple variables into shared memory
Hello, I'm trying to write multiple variables one by one into a mapped_region, but somehow I fail. Here is the Code I worked on so far: boost::interprocess::shared_memory_object shm_obj(boost::interprocess::open_or_create, "Test", boost::interprocess::read_write); shm_obj.truncate(1000); boost::interprocess::mapped_region region(shm_obj, boost::interprocess::read_write, 0, 0); uint32_t size = 33; uint32_t type = 666; std::string data("I'm a Test!"); uint32_t* start_ptr_int = &(static_cast<uint32_t*> (region.get_address())[0]); std::memcpy(start_ptr_int, &size, sizeof (uint32_t)); start_ptr_int = &(static_cast<uint32_t*> (region.get_address())[4]); std::memcpy(start_ptr_int, &type, sizeof (uint32_t)); char* start_ptr_char = &(static_cast<char*> (region.get_address())[8]); std::memcpy(start_ptr_char, data.c_str(), data.length()); uint32_t sizeOfData; std::memcpy(&sizeOfData, &(static_cast<uint32_t*> (region.get_address())[0]), sizeof (uint32_t)); uint32_t typeOfData; std::memcpy(&typeOfData, &(static_cast<uint32_t*> (region.get_address())[4]), 4); char* revData = new char[sizeOfData]; std::memcpy(revData, &(static_cast<char*> (region.get_address())[sizeof (uint32_t) * 2]), sizeOfData); First I write an uint32_t into the region at index 0. Afterwards, I'm writing another uint32_t at position 4 (assuming the first uint32_t took the bytes 0 - 3). Last I write a string into the region, at index 8. Reading the first uint32_t again from shared memory works fine, as does reading the string (the last variable). But if I try to read the second variable (uint32_t) the data is not what I expected. I know that the problem is memcpy, somehow my syntax does not work. Unfortunately I could not find a matching example, cause mostly memset is used to explain the functionality of boost::mapped_region. I have to write lots of data into shared memory (structured like size1|type1|data1|...|sizeN|typeN|dataN) but still can't figure out how to do this the right way. Any suggestions are greatly appreciated. Kind regards Michael Behnke -- Bachelorand ________________________________________________________ ADITION technologies AG Schwarzwaldstraße 78b 79117 Freiburg http://www.adition.com T +49 / (0)761 / 88147 - 30 F +49 / (0)761 / 88147 - 77 SUPPORT +49 / (0)1805 - ADITION (Festnetzpreis 14 ct/min; Mobilfunkpreise maximal 42 ct/min) Eingetragen beim Amtsgericht Düsseldorf unter HRB 54076 Vorstände: Andreas Kleiser, Jörg Klekamp, Marcus Schlüter Aufsichtsratsvorsitzender: Joachim Schneidmadl UStIDNr.: DE 218 858 434
participants (1)
-
Michael Behnke