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. how to disable structure padding in qt uisng mingw

how to disable structure padding in qt uisng mingw

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 508 Views
  • 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.
  • P Offline
    P Offline
    Parvathy2020
    wrote on 1 Jun 2023, 08:52 last edited by
    #1

    I have structure with with 2 members one with data

    struct xxx
    {
    std::uint32_t x;
    std::uint8_t y;
    };

    Now the issue is even if variable y is declared as 8 bit integer it is also occupying 32 bit. What I understood is , it is because of structure padding to maintain memory alignment.
    Issue got resolved after adding #pragma pack(push,1)

    Is there any way to disable structure padding using some qt creator or compiler settings ?

    J 1 Reply Last reply 1 Jun 2023, 08:56
    0
    • P Parvathy2020
      1 Jun 2023, 09:17

      @jsulm Hi...thanks for the quick reply. But i just want a solution to handle this by compiler by its own other than me adding pragma manually.

      If not qt, can we configure mingw compiler to handle it . Any alternative solution other than adding it in code.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Jun 2023, 09:52 last edited by
      #4

      @Parvathy2020 said in how to disable structure padding in qt uisng mingw:

      But i just want a solution to handle this by compiler by its own other than me adding pragma manually

      #pragma is a hint for compiler. Compiler will not do it by default as in most cases it is not needed and reduces performance (so, it is a bad idea to enable it by default for all structs and I'm not aware of such a flag for the compiler). So, if you want to pack a struct then you have to use pragma, don't know why this is an issue for you.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 1 Jun 2023, 10:04
      0
      • P Parvathy2020
        1 Jun 2023, 08:52

        I have structure with with 2 members one with data

        struct xxx
        {
        std::uint32_t x;
        std::uint8_t y;
        };

        Now the issue is even if variable y is declared as 8 bit integer it is also occupying 32 bit. What I understood is , it is because of structure padding to maintain memory alignment.
        Issue got resolved after adding #pragma pack(push,1)

        Is there any way to disable structure padding using some qt creator or compiler settings ?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Jun 2023, 08:56 last edited by
        #2

        @Parvathy2020 said in how to disable structure padding in qt uisng mingw:

        Is there any way to disable structure padding using some qt creator or compiler settings ?

        This has nothing to do with Qt.
        You already found the correct solution using #pragma.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply 1 Jun 2023, 09:17
        1
        • J jsulm
          1 Jun 2023, 08:56

          @Parvathy2020 said in how to disable structure padding in qt uisng mingw:

          Is there any way to disable structure padding using some qt creator or compiler settings ?

          This has nothing to do with Qt.
          You already found the correct solution using #pragma.

          P Offline
          P Offline
          Parvathy2020
          wrote on 1 Jun 2023, 09:17 last edited by
          #3

          @jsulm Hi...thanks for the quick reply. But i just want a solution to handle this by compiler by its own other than me adding pragma manually.

          If not qt, can we configure mingw compiler to handle it . Any alternative solution other than adding it in code.

          J 1 Reply Last reply 1 Jun 2023, 09:52
          0
          • P Parvathy2020
            1 Jun 2023, 09:17

            @jsulm Hi...thanks for the quick reply. But i just want a solution to handle this by compiler by its own other than me adding pragma manually.

            If not qt, can we configure mingw compiler to handle it . Any alternative solution other than adding it in code.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 1 Jun 2023, 09:52 last edited by
            #4

            @Parvathy2020 said in how to disable structure padding in qt uisng mingw:

            But i just want a solution to handle this by compiler by its own other than me adding pragma manually

            #pragma is a hint for compiler. Compiler will not do it by default as in most cases it is not needed and reduces performance (so, it is a bad idea to enable it by default for all structs and I'm not aware of such a flag for the compiler). So, if you want to pack a struct then you have to use pragma, don't know why this is an issue for you.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            P 1 Reply Last reply 1 Jun 2023, 10:04
            0
            • J jsulm
              1 Jun 2023, 09:52

              @Parvathy2020 said in how to disable structure padding in qt uisng mingw:

              But i just want a solution to handle this by compiler by its own other than me adding pragma manually

              #pragma is a hint for compiler. Compiler will not do it by default as in most cases it is not needed and reduces performance (so, it is a bad idea to enable it by default for all structs and I'm not aware of such a flag for the compiler). So, if you want to pack a struct then you have to use pragma, don't know why this is an issue for you.

              P Offline
              P Offline
              Parvathy2020
              wrote on 1 Jun 2023, 10:04 last edited by
              #5

              @jsulm Thank you !

              In my case this structure is auto - generated from a json file using some script. That is the challenge here- manually adding pragma to those auto generated structures.

              1 Reply Last reply
              0
              • P Parvathy2020 has marked this topic as solved on 1 Jun 2023, 10:42

              1/5

              1 Jun 2023, 08:52

              • Login

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