Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory alignment __attribute__( ( aligned ( 8 ) ) )

Tags:

c

I got a program in a book

#include <stdio.h>
int main( )
{
    struct data
    {
        int a    __attribute__( ( aligned ( 8 ) ) ) ;
        char ch  __attribute__( ( aligned ( 1 ) ) ) ;
        float s  __attribute__( ( aligned ( 4 ) ) ) ;    
    } ; 
    struct data e ;
    printf ( "\n%u %u %u", &e.a, &e.ch, &e.s ) ;
    printf ( "\n%d", sizeof ( e ) ) ;
    return 0 ;
}

when I run it on cygwin installed on windows 7 machine. I am getting output

2280712 2280716 2280720 16

why this output I am getting? I was expecting the output

2280712 2280720 2280721 13

like image 694
user555735 Avatar asked Dec 28 '10 07:12

user555735


People also ask

What does attribute aligned do?

The aligned attribute only increases the alignment for a struct or struct member. For a variable that is not in a structure, the minimum alignment is the natural alignment of the variable type. To set the alignment in a structure to any value greater than 0, use the packed variable attribute.

What does __ Attribute__ packed )) Meaning?

__attribute__((packed)) variable attributeThe attribute specifies that a member field has the smallest possible alignment. That is, one byte for a variable field, and one bit for a bitfield, unless you specify a larger value with the aligned attribute.

What is memory alignment in assembly?

Alignment refers to the arrangement of data in memory, and specifically deals with the issue of accessing data as proper units of information from main memory. First we must conceptualize main memory as a contiguous block of consecutive memory locations. Each location contains a fixed number of bits.

What is structure alignment in C?

Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment.


2 Answers

A sizeof is always a multiple of largest alignment, as it is actually reports 'step' in chars in array of given type which include padding as between members so padding between array elements required to align them.

Memory layout of struct data will look like this:

 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 ... <--offset
|                                               |
|          struct data   (element [0])          |      (element [1])
|                                               |
|     a     |ch|::::::::|     s     |:::::::::::|     a     |ch|::::...
|                                               |
|<----------- sizeof(struct data) ------------->|

|::::| is padding
  • a is at offset 0 which is obviously a multiple of 8;
  • ch is at offset 4 which is (also obviously) a multiple of 1;
  • s is at offset 8 which is multiple of 4;
  • sizeof(struct data) is equal to offset of [1] (16) which required to be multiple of max(8,1,4) = 8
like image 187
Vovanium Avatar answered Oct 21 '22 03:10

Vovanium


The aligned(N) attribute aligns the current data item on an address which is a multiple of N, by inserting padding bytes before the data item. You appear to expect that it does something with packing, which is different.

Your structure appears to be laid out as follows:

Address  Member
-------  -------
2280712  a        [address aligned on multiple of 8]
2280713  a
2280714  a
2280715  a
2280716  ch       [address aligned on multiple of 1]
2280717  (unused)
2280718  (unused)
2280719  (unused)
2280720  s        [address aligned on multiple of 4]
2280721  s
2280722  s
2280723  s
like image 14
Greg Hewgill Avatar answered Oct 21 '22 02:10

Greg Hewgill