Andes Workshop

It is currently Fri Mar 29, 2024 2:23 am

All times are UTC + 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Size and Byte Alignment of Primitive Data Types
PostPosted: Tue May 06, 2014 2:58 pm 
Offline

Joined: Wed Apr 13, 2011 7:44 pm
Posts: 23
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 6848 times ]

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

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


Top
 Profile Send private message E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group