Thread: what is aligned and unaligned data in c

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Smile what is aligned and unaligned data in c

    what is aligned and unaligned data in c and what's the use of it.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The compiler will align variables depending on architecture requirements, or just to improve performance. For a code illustration, run the following:

    Code:
    #include <stdio.h>
    
    struct s
    {
        char c;
        int i;
    };
    
    int main(void)
    {
        printf("size: %u\n", sizeof(struct s));
        return 0;
    }
    If you your machine uses 4 byte integers (like most machines), then you might expect the structure to be a size of 5 in the above code. Chances are that if you compile and run that, you will get 8 bytes as the result. Why? Because the compiler aligns the integer on 4 byte boundaries, so there are 3 unused bytes between the character and the integer.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof applied to structure...
    By roaan in forum C Programming
    Replies: 26
    Last Post: 08-01-2009, 01:09 AM
  2. Problem with unaligned intrinsics
    By The Wazaa in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2009, 12:36 PM
  3. Structure Padding in RISC and CISC
    By karthik537 in forum C Programming
    Replies: 3
    Last Post: 01-20-2009, 03:12 AM
  4. Loading files into memory
    By アストラル in forum C++ Programming
    Replies: 22
    Last Post: 09-20-2008, 04:49 PM
  5. Fast memcpy for unaligned addresses
    By Pea in forum C Programming
    Replies: 10
    Last Post: 01-23-2005, 03:31 PM