Visit Our UNIX and Linux User Community

Address Alignment rules for structure variables

Thread Tools Search this Thread
Top Forums Programming Address Alignment rules for structure variables
# 1  
Old 03-25-2004
Java Address Alignment rules for structure variables


This question might look naive, but I need to know address alignment rules used by O/S, hence posting this one.

I've following 2 structures :-

struct xyz {
char a;
int b;
char c;

struct abc {
char a;
char c;
int b;

If I print size of these 2 structures using sizeof() operator, it is 12 for xyz & 8 for abc. This is consistent on 32 as well as 64 bit machines (I'm using HP-UX).

Both structures have some no of elements of same type, only they differ in order of definition.

Then why is that size of struct xyz is 12 where as that of struct abc is 8 ?

What are precise rules for address alignment ?

Thanks in advance
# 2  
Old 03-25-2004
I tried it on HP uX11 and get the same results, tried to swap the definitions of the structures but still gives the same output.
# 3  
Old 03-29-2004
Re: Address Alignment rules for structure variables

Originally posted by anijog

Then why is that size of struct xyz is 12 where as that of struct abc is 8 ?

What are precise rules for address alignment ?

Thanks in advance
The first question has been covered, mostly...but it is due to the natural word size and alignment of the machine you are compiling on. However, because of this, I would have to say that there are no "precise rules" that can be assumed for every machine. If you want your code to be portable then you can not make any assumptions other than that the elements will be ordered in the same order as they are in the structure.

So, reading beyond your question a bit, I'd ask why you need to know these precise rules when such operands as "offsetof" and "sizeof" exist to give you this information.

The only unaligned data type is char, so if you mix any other types into your structure you may have alignment differences on various machines...therefore good code should either make so assumption and use offsetof to aquire the aligned location of the member, or should not not require the offset and make direct access to the members as provided by the compiler.

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with awk alignment

Dear All, I am in the beginning stage of learning shell scripting and preparing shell script on my own now. I would like to get help from fellow mates here. As I am trying to take O/P with space included from I/P table. Kindly guide me to align given I/P table as Expected O/P. ... (5 Replies)
Discussion started by: Raja007
5 Replies

2. HP-UX

HP-UX text alignment

HI all, I tried to edit my files using HP-UX but my output tends to not align when I add another character in the files to edit my files i used the command is as follow chmod +w filename vi filename Help, :eek: (1 Reply)
Discussion started by: jasonhpwong
1 Replies

3. Shell Programming and Scripting


Hi, I am having a file with with format. however, for longer xml, the xml code has been truncated like this. F1 |###################### |String1 |<XML><REQ><MSGTYPE>DBDIRECT</MSGTYPE><SYNC>0</SYNC><CLIENT>C11</CLIENT>NAME=MYNAME|JOB=MYJOB| | ... (3 Replies)
Discussion started by: shellwell
3 Replies

4. UNIX for Dummies Questions & Answers

VI paste out of alignment

We have a guy at work who is trying to copy and paste from one file to another using vi and highlighting the code to copy with a mouse. Source file: xyz 123 abc 999 zyx 321 cba 999 xyz 123 abc 999 But when he pastes it he gets (I put the underlines in to show... (4 Replies)
Discussion started by: dlam
4 Replies

5. Shell Programming and Scripting

alignment of variable

Dear Champs, i have a file let a.txt having value number text 00 123 012 145 456 ...etc i need number and text column vales should right align ??? how can i achive this ??? NOTE number is of max 3 char and text can take max 7 if any records are less than above lengths... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

6. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

7. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

8. Programming

how to round up a memory address(memory alignment problem)

Hi, I try to marshal a unsigned int and a char * into a buffer, and then unmarshal them later to get them out. I need to put the char * in the front and unsigned int at the end of the buffer. However, my system always give me "BUS ERROR". I am using Sun Sparcs Sloris 2.10. My code to marshal... (6 Replies)
Discussion started by: nj302
6 Replies

9. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

Featured Tech Videos