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. [SOLVED]QByteArray, pointer as data
Qt 6.11 is out! See what's new in the release blog

[SOLVED]QByteArray, pointer as data

Scheduled Pinned Locked Moved General and Desktop
14 Posts 6 Posters 13.4k 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.
  • D Offline
    D Offline
    DerManu
    wrote on last edited by
    #4

    [quote author="Mohsen" date="1348287297"]i don't know what's really the aim doing such unstable job since pointers would be recreated or deleted anytime. anyway you may do something like this
    @ int address=(int)SOURCE;
    @[/quote]

    This will lead to problems on different architectures (32-/64-Bit). Use quintptr instead.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #5

      bq. Could you explain your needs, and why you want to convert the pointer into bytes? bq.

      I need it because data will be sent via drag and drop event, where data is sent as QByteArray.

      This pointers will stay in memory as long as the program remains open. So there won't be a problem with pointers being created or deleted.

      And there shouldn't be a problem in different architectures. Program compiled as 32-bit will be 32-bit even on 64-bit machines.

      @Mohsen
      You solution works perfectly. Thanks!

      Regards,
      Jake

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DerManu
        wrote on last edited by
        #6

        [quote author="Jake007" date="1348315792"]
        And there shouldn't be a problem in different architectures. Program compiled as 32-bit will be 32-bit even on 64-bit machines.
        @Mohsen
        You solution works perfectly. Thanks![/quote]

        Well "there shouldn't be a problem" says it all. Why introduce the potential to fail in future releases when you decide you might want native 64-bit applications, in a time where you've long forgotten about how you pass those pointers around. Just use quintptr like I proposed. Everything else stays the same, it just has benefits: it's much more future-proof and the programmer's intention is more clear than with int.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #7

          Because int in 64-bit compile is 8 Bytes large :). As much as a pointer.

          And I considered your proposal the first time and used it in Mohsen's solution :).

          Regards,
          Jake

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #8

            Actually IIRC a int is defined to be at least 16bit long... so storing a pointer in an int is relying on undefined behavior -- even on 32bit machines!

            Relying on undefined behavior is giving the compiler a free hand to break (== optimize) your code however it sees fit! So even if your code works today, it might break whenever you upgrade your compiler...

            1 Reply Last reply
            0
            • T Offline
              T Offline
              twsimpson
              wrote on last edited by
              #9

              What size int is and what the machine word length is are unrelated. It just so happened that "int" happens to be 32bit in most common architectures, but "quintptr":http://qt-project.org/doc/qt-4.8/qtglobal.html#quintptr-typedef is "guaranteed to be the same size as a pointer on all platforms supported by Qt."

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mohsen
                wrote on last edited by
                #10

                [quote author="Jake007" date="1348315792"]
                @Mohsen
                You solution works perfectly. Thanks!
                [/quote]
                you're welcome

                [quote author="DerManu" date="1348314572"]
                This will lead to problems on different architectures (32-/64-Bit). Use quintptr instead.[/quote]
                you're right. that was only an example. you would try it with quintptr

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DerManu
                  wrote on last edited by
                  #11

                  [quote author="Jake007" date="1348318060"]Because int in 64-bit compile is 8 Bytes large :). As much as a pointer.

                  And I considered your proposal the first time and used it in Mohsen's solution :).

                  Regards,
                  Jake[/quote]

                  Whatever. If people deliberately chose to write bad software, I can't change it, and it explains alot of what's going on in the software world. Your assumption about ints on 64 bit is wrong. See (L)LP64 data model.

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #12

                    It's not an assumption. sizeof(int) wrote out 8. Unless I did something wrong (button run instead of build and run etc... happens to me a lot lately).

                    I did use quintptr in the first place as suggested. I also wrote that I used it ( unless I awkwardly expressed myself).
                    And I don't see the problem with "deliberately".

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DerManu
                      wrote on last edited by
                      #13

                      [quote author="Jake007" date="1348336462"]It's not an assumption. sizeof(int) wrote out 8. Unless I did something wrong (button run instead of build and run etc... happens to me a lot lately).[/quote]
                      Then you were on a system that uses ILP64 (int, long, pointer are 64 bit). Most 64-bit systems use LP64 (long, pointer are 64 bit) or even LLP64 (longlong, pointer are 64 bit) though.

                      [quote author="Jake007" date="1348336462"]I did use quintptr in the first place as suggested. I also wrote that I used it ( unless I awkwardly expressed myself)[/quote]
                      Excellent :). I misunderstood it as "I considered your suggestion but used Mohsen's original suggestion with int instead".

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #14

                        I just tried sizeof(int) again ( on my computer this time) under x64 compile and the result was 4.
                        It's possible that I was on ILP64 system, or that it was my mistake. Probably second. Today I first heard about LP models.

                        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