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
Forum Updated to NodeBB v4.3 + New Features

how to disable structure padding in qt uisng mingw

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 522 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 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 ?

    jsulmJ 1 Reply Last reply
    0
    • P Parvathy2020

      @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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • P Parvathy2020

        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 ?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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
        • jsulmJ jsulm

          @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 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.

          jsulmJ 1 Reply Last reply
          0
          • P Parvathy2020

            @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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            0
            • jsulmJ jsulm

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

              • Login

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