Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Can anybody explain below statment ?
Forum Updated to NodeBB v4.3 + New Features

Can anybody explain below statment ?

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 3 Posters 453 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1
    uint8_t Can1MessageRAM[712U] __attribute__((aligned(32)));
    

    i want to understand what is purpose of attribute ?

    if possible anybody can explain above sentence ?

    D Chris KawaC 2 Replies Last reply
    0
    • Q Qt embedded developer
      uint8_t Can1MessageRAM[712U] __attribute__((aligned(32)));
      

      i want to understand what is purpose of attribute ?

      if possible anybody can explain above sentence ?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #3

      All symbols in C++ that start with __ are implementation specified. In other words they are not portable. __attribute__((ATTRIBUTE)) is a GNU extension of C++ that lets you put non-standard attributes on types. One of those attributes used here is alignment. Other vendors have other means of doing that. For example MSVC has #pragma pack(32) for the same thing. Starting from C++11 we also have a standard way of specifying alignment with alignas(32), so these vendor specific extensions are no longer needed, but a lot of older code still uses them.

      As for alignment itself that just means that the variable will be placed at an address aligned to the value specified. In other words if you print the address of the variable Can1MessageRAM it's going to be a multiple of 32. If you place two class members with alignment attribute the compiler might pad the space between them so that their address is the multiple of specified value.

      For example if you had a struct like this:

      struct A
      {
          uint8_t Can1MessageRAM1[712U];
          uint8_t Can1MessageRAM2[712U];
      };
      

      and printed the offsets of its fields:

      qDebug() << offsetof(A, Can1MessageRAM1) << offsetof(A, Can1MessageRAM2);
      

      a possible output would be 0 712 i.e. the second member starts right after the first. If you align them like this:

      struct A
      {
          alignas(32) uint8_t Can1MessageRAM1[712U];
          alignas(32) uint8_t Can1MessageRAM2[712U];
      };
      

      the output may change to be like this: 0 736, so compiler inserts some space between the two members, so that the address of the second is aligned to 32.

      Alignment has many uses. Some techniques, like using SSE requires aligned variables to work. On some hardware unaligned access is less performant because there's no dedicated silicon in the CPU, so it has to compute the unaligned address or split operation to aligned parts. Another usage is when you have a platform with fixed memory addresses mapping to specific hardware functions. Yet another use of aligned data is when you iterate over large dataset. Aligned elements might fit better into a cache line improving access performance. Many other uses exist.

      1 Reply Last reply
      5
      • Q Qt embedded developer
        uint8_t Can1MessageRAM[712U] __attribute__((aligned(32)));
        

        i want to understand what is purpose of attribute ?

        if possible anybody can explain above sentence ?

        D Offline
        D Offline
        DerReisende
        wrote on last edited by
        #2

        @Qt-embedded-developer See e.g. here.

        1 Reply Last reply
        3
        • Q Qt embedded developer
          uint8_t Can1MessageRAM[712U] __attribute__((aligned(32)));
          

          i want to understand what is purpose of attribute ?

          if possible anybody can explain above sentence ?

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #3

          All symbols in C++ that start with __ are implementation specified. In other words they are not portable. __attribute__((ATTRIBUTE)) is a GNU extension of C++ that lets you put non-standard attributes on types. One of those attributes used here is alignment. Other vendors have other means of doing that. For example MSVC has #pragma pack(32) for the same thing. Starting from C++11 we also have a standard way of specifying alignment with alignas(32), so these vendor specific extensions are no longer needed, but a lot of older code still uses them.

          As for alignment itself that just means that the variable will be placed at an address aligned to the value specified. In other words if you print the address of the variable Can1MessageRAM it's going to be a multiple of 32. If you place two class members with alignment attribute the compiler might pad the space between them so that their address is the multiple of specified value.

          For example if you had a struct like this:

          struct A
          {
              uint8_t Can1MessageRAM1[712U];
              uint8_t Can1MessageRAM2[712U];
          };
          

          and printed the offsets of its fields:

          qDebug() << offsetof(A, Can1MessageRAM1) << offsetof(A, Can1MessageRAM2);
          

          a possible output would be 0 712 i.e. the second member starts right after the first. If you align them like this:

          struct A
          {
              alignas(32) uint8_t Can1MessageRAM1[712U];
              alignas(32) uint8_t Can1MessageRAM2[712U];
          };
          

          the output may change to be like this: 0 736, so compiler inserts some space between the two members, so that the address of the second is aligned to 32.

          Alignment has many uses. Some techniques, like using SSE requires aligned variables to work. On some hardware unaligned access is less performant because there's no dedicated silicon in the CPU, so it has to compute the unaligned address or split operation to aligned parts. Another usage is when you have a platform with fixed memory addresses mapping to specific hardware functions. Yet another use of aligned data is when you iterate over large dataset. Aligned elements might fit better into a cache line improving access performance. Many other uses exist.

          1 Reply Last reply
          5
          • Q Qt embedded developer 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