Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Structure Alignment?

Structure Alignment?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 4 Posters 0 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.
  • B Offline
    B Offline
    badal_akr
    wrote on last edited by
    #1

    Can any body tell me why structure alignment is necessary in C/C++ and what it may cause? Anil Kumar

    H 2 Replies Last reply
    0
    • B badal_akr

      Can any body tell me why structure alignment is necessary in C/C++ and what it may cause? Anil Kumar

      H Offline
      H Offline
      Halawlaws
      wrote on last edited by
      #2

      Hi Applications should generally align structure members at addresses that are "natural" for the data type and the processor involved. For example, a 4-byte data member should have an address that is a multiple of four. This principle is especially important when you write code for porting to multiple processors. A misaligned 4-byte data member, which is on an address that is not a multiple of four, causes a performance penalty with an 80386 processor and a hardware exception with a MIPSĀ® RISC processor. In the latter case, although the system handles the exception, the performance penalty is significantly greater. /\|-||\/|/\|)

      1 Reply Last reply
      0
      • B badal_akr

        Can any body tell me why structure alignment is necessary in C/C++ and what it may cause? Anil Kumar

        H Offline
        H Offline
        Halawlaws
        wrote on last edited by
        #3

        Or maybe this can help too: All modern CPUs expect that fundamental types like ints, longs and floats will be stored in memory at addresses that are multiples of their length. CPUs are optimized for accessing memory aligned in this way. Some CPUs: allow unaligned access but at a performance penalty; trap unaligned accesses to the operating system where they can either be ignored, simulated or reported as errors; use unaligned addresses as a means of doing special operations during the load or store. When a C compiler processes a structure declaration, it can: add extra bytes between the fields to ensure that all fields requiring alignment are properly aligned; ensure that instances of the structure as a whole are properly aligned. Malloc always returns memory pointers that are aligned for the strictest, fundamental machine type. The specifications for C/C++ state that the existence and nature of these padding bytes are implementation defined. This means that each CPU/OS/Compiler combination is free to use whatever alignment and padding rules are best for their purposes. Programmers however are not supposed to assume that specific padding and alignment rules will be followed. There are no controls defined within the language for indicating special handling of alignment and padding although many compilers like gcc have non-standard extensions to permit this. Summary Structure Alignment Structure alignment may be defined as the choice of rules which determine when and where padding is inserted together with the optimizations which the compiler is able to effect in generated code. /\|-||\/|/\|)

        B 1 Reply Last reply
        0
        • H Halawlaws

          Or maybe this can help too: All modern CPUs expect that fundamental types like ints, longs and floats will be stored in memory at addresses that are multiples of their length. CPUs are optimized for accessing memory aligned in this way. Some CPUs: allow unaligned access but at a performance penalty; trap unaligned accesses to the operating system where they can either be ignored, simulated or reported as errors; use unaligned addresses as a means of doing special operations during the load or store. When a C compiler processes a structure declaration, it can: add extra bytes between the fields to ensure that all fields requiring alignment are properly aligned; ensure that instances of the structure as a whole are properly aligned. Malloc always returns memory pointers that are aligned for the strictest, fundamental machine type. The specifications for C/C++ state that the existence and nature of these padding bytes are implementation defined. This means that each CPU/OS/Compiler combination is free to use whatever alignment and padding rules are best for their purposes. Programmers however are not supposed to assume that specific padding and alignment rules will be followed. There are no controls defined within the language for indicating special handling of alignment and padding although many compilers like gcc have non-standard extensions to permit this. Summary Structure Alignment Structure alignment may be defined as the choice of rules which determine when and where padding is inserted together with the optimizations which the compiler is able to effect in generated code. /\|-||\/|/\|)

          B Offline
          B Offline
          badal_akr
          wrote on last edited by
          #4

          But as per my understanding the structure members are stored in the contigous memory locations for example struct temp { char a, int b, float c, }; if i declare temp *structPtr = 0x00000000 (Address of element a) then what should be the address of element b if it is 0x00000002 then one byte is waisted. According to you b should have address of multiple of 4. So i can say that there is a trade of CPU performance vs Memory usage Am i right please guide me. Regards Anil Kumar

          B 1 Reply Last reply
          0
          • B badal_akr

            But as per my understanding the structure members are stored in the contigous memory locations for example struct temp { char a, int b, float c, }; if i declare temp *structPtr = 0x00000000 (Address of element a) then what should be the address of element b if it is 0x00000002 then one byte is waisted. According to you b should have address of multiple of 4. So i can say that there is a trade of CPU performance vs Memory usage Am i right please guide me. Regards Anil Kumar

            B Offline
            B Offline
            Blake Miller
            wrote on last edited by
            #5

            Yes, memory usage versus performance. In Visual Studio you can use the #pragma pack() to control memory alignment on a structure-by-structure basis.

            G 1 Reply Last reply
            0
            • B Blake Miller

              Yes, memory usage versus performance. In Visual Studio you can use the #pragma pack() to control memory alignment on a structure-by-structure basis.

              G Offline
              G Offline
              GKarRacer
              wrote on last edited by
              #6

              Generally the default structure alignment (which you should use unless there is a compelling reason not to) is 8 bytes. This aligns all basic data types (up to doubles) to even multiples of their size. Example (using 8 byte alignment):

              struct test
              {
              char a;
              int b;
              WORD c;
              char d;
              double e;
              };

              The offset addresses are: a = 0 b = 4 c = 8 d = 10 e = 16 Total size of the structure: 24 bytes Notice there is some wasted space in there. You can add some variables to this and not increase the size of the structure at all. The following structure is still 24 bytes in size, but is now fully packed.

              struct test2
              {
              char a;
              char a2;
              WORD a3;
              int b;
              WORD c;
              char d;
              char d2;
              int d3;
              double e;
              };

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups