Results 1 to 2 of 2

Thread: (noob question) write qint64 into qsharedmemory

  1. #1
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default (noob question) write qint64 into qsharedmemory

    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????

  2. #2
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: (noob question) write qint64 into qsharedmemory

    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.

    Qt Code:
    1. qint64 yourQInt64 = ...;
    2. qint64* placeToWriteQInt64 = (qint64*)sharedMemory.data();
    3. *placeToWriteQInt64 = yourQInt64;
    To copy to clipboard, switch view to plain text mode 


    Btw., do not use this code:

    Qt Code:
    1. allplayercount = (qint64)(numofallplayers.data());
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. qint64 allplayercount = *((qint64*)numofallplayers.data());
    To copy to clipboard, switch view to plain text mode 
    Last edited by Vit Stepanek; 28th June 2010 at 11:41. Reason: updated contents

  3. The following user says thank you to Vit Stepanek for this useful post:

    daemonna (28th June 2010)

Similar Threads

  1. new vs QSharedMemory
    By JovianGhost in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2010, 00:34
  2. QWizard noob question
    By Ossi in forum Newbie
    Replies: 5
    Last Post: 2nd September 2009, 12:30
  3. a simple noob question
    By wrproject in forum Newbie
    Replies: 1
    Last Post: 1st July 2008, 23:25
  4. QSlider and qint64 values
    By kemp in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2007, 20:19
  5. qint64 to QString
    By Morea in forum Newbie
    Replies: 1
    Last Post: 24th March 2006, 08:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.