Andes Workshop
http://forum.andestech.com/

Size and Byte Alignment of Primitive Data Types
http://forum.andestech.com/viewtopic.php?f=25&t=841
Page 1 of 1

Author:  ianfeng [ Tue May 06, 2014 2:58 pm ]
Post subject:  Size and Byte Alignment of Primitive Data Types

Code:
#include <stdio.h>

struct _test {
  char c0;
  short s0;
  char c1;
  int i0;
  char c2;
  float f0;
  char c3;
  char c4;
} test;

int main() {
   test.c0 = 'a';
   test.s0 = 0x1234;
   test.c1 = 'b';
   test.i0 = 0x12345678;
   test.c2 = 'c';
   test.f0 = 12.34;
   test.c3 = 'd';
   test.c4 = 'e';
   printf(" address is: 0x%x  - char  1-byte alignment\n", (unsigned int) &test.c0);
   printf(" address is: 0x%x  - short 2-byte alignment\n", (unsigned int) &test.s0);
   printf(" address is: 0x%x  - char  1-byte alignment\n", (unsigned int) &test.c1);
   printf(" address is: 0x%x  - int   4-byte alignment\n", (unsigned int) &test.i0);
   printf(" address is: 0x%x  - char  1-byte alignment\n", (unsigned int) &test.c2);
   printf(" address is: 0x%x  - float 4/8-byte alignment\n", (unsigned int) &test.f0);
   printf("              base on single/double precision\n");
   printf(" address is: 0x%x  - char  1-byte alignment\n", (unsigned int) &test.c3);
   printf(" address is: 0x%x  - char  1-byte alignment\n", (unsigned int) &test.c4);
   return 0;
}

The output:
Code:
address is: 0x3010fc  - char  1-byte alignment
address is: 0x3010fe  - short 2-byte alignment
address is: 0x301100  - char  1-byte alignment
address is: 0x301104  - int   4-byte alignment
address is: 0x301108  - char  1-byte alignment
address is: 0x30110c  - float 4/8-byte alignment
              base on single/double precision
address is: 0x301110  - char  1-byte alignment
address is: 0x301111  - char  1-byte alignment

Attachment:
data_types.jpg
data_types.jpg [ 70.43 KiB | Viewed 3335 times ]

Attachment:
struct_example.zip [27.87 KiB]
Downloaded 265 times

For detail message, please refer programming guide chapter "8.1.2 Primitive Data Types".

Page 1 of 1 All times are UTC + 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/