Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Memory access by address
Qt 6.11 is out! See what's new in the release blog

Memory access by address

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mohmo
    wrote on last edited by
    #1

    hey bros;
    I have a program to access the variables by their addresses and this is my code:

    float f;
    int t;

    QHash <int,int*> hash;
    hash.insert(1,&t);
    hash.insert(2,(int*)&f);

    *hash.value(1)=34;
    qDebug()<<"t1="<<t1; //t1= 34

    *hash.value(2)=456.987;
    qDebug()<<"f="<<f; //f=7.94536e-43 !!!!!!

    why this code is not working? and how can i fix that?
    Actually the objective of this code is to indexing every variable with any type (int, float, structure, Boolean, .. ) by an integer number. Hashing is used to achieve this goal.
    Thanks a lot

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohmo
      wrote on last edited by
      #2

      If i use QVariant like below:

      @QHash <int,QVariant*> hash;
      @
      and i insert the same values into it, i encounter this error:

      : error: C2664: 'QHash<Key,T>::insert' : cannot convert parameter 2 from 'int *' to 'QVariant *const &'
      with
      [
      Key=int,
      T=QVariant *
      ]

      But if i use the previous code and retrieve the address as below:
      @_(float_)hash.value(2)=456.987;
      qDebug()<<“f=”<<f; //f=456.987@

      everything will be OK.

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hi,
        Better to not use pointers to your variables for this. A QVariant will use the same amount of memory space as the variable it will point to. So, the QHash should have an <int, QVariant> not the pointer. Then add the variable to the QVariant via a constructor or other variant function and you should be able to convert any variable into that QHash. Even mix them if needed.

        Greetz, Jeroen

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mohmo
          wrote on last edited by
          #4

          hey Jeroentje@home
          thanks for your reply But could you please make an example of your plan?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Jeroentje@home meant that you should follow koahnig's example

            @
            QHash<int, QVariant> hash;
            float fInput= 1.2
            hash.insert(1, fInput);
            float myFloat = hash.value(1).toFloat();
            @

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved