PDA

View Full Version : (noob question) write qint64 into qsharedmemory



daemonna
28th June 2010, 06:10
as found in examples

QSharedMemory sharedMemory("foobar");
sharedMemory.create(1024);
sharedMemory.lock();
char *to = (char*)sharedMemory.data();
char *text = "hello world";
memcpy(to, text, strlen(text)+1);
sharedMemory.unlock();

as far i understand, this will copy char/string 'hello world' into shared memory... what if i want shared memory to hold qint64? i guess i know how to read

if(!numofallplayers.attach(QSharedMemory::ReadWrit e)){
//some error
}
numofallplayers.lock();
allplayercount = (qint64)(numofallplayers.data());
numofallplayers.unlock();


but how i'm gonna write qint64 to it????

Vit Stepanek
28th June 2010, 12:28
QSharedMemory::data method returns a void*, so it's just a pointer to a bytes array, where you can place anything as you like. Copying integer types is even easier.



qint64 yourQInt64 = ...;
qint64* placeToWriteQInt64 = (qint64*)sharedMemory.data();
*placeToWriteQInt64 = yourQInt64;



Btw., do not use this code:


allplayercount = (qint64)(numofallplayers.data());

This way you write a void*, the pure address, to a qint64, not a value stored in that place in a memory.
Use rather this:



qint64 allplayercount = *((qint64*)numofallplayers.data());