Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is the use of align in C/C++ and what is it benefit?

Status
Not open for further replies.

sukhdeepmankoo

Member level 2
Joined
May 14, 2009
Messages
47
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,616
Hi,
I have read that __align is used to set the boundary of variable, what is its benefit.
suppose __align(2) unsigned short u;

even unsigned short variable can have its value from 0 to 65535. then where is difference lies.
second thing, can we say that __align is a qualifier in c/c++.
Thanks.
 

Re: align in C/C++

when you say
" unsigned short u"
it takes two bytes in memory.

with
"align(2) unsigned short u"
it still takes two bytes in memory.

the difference is 'at what address the "twobyte chunk" starts.

if the address starts at the processors natural accessing for the bytes,
then it executes in one cycle.

if it starts in unnatural(i.e, unaligned) it takes more than one cycle ,
which slows down the execution.


eg:

addr0 byte0
addr1 byte1 ---no problem :bytes are aligned

addr0 ---
addr1 byte0
addr2 byte1 ---now the processor reads two times.
addr2 ---

first addr0 and 1---get the byte0
second addr1 and 2---get byte1

so by specifying "align"
you ask the compiler to put the data in natural boundaries of the processor.


srizbf
22ndjune2010
 

align in C/C++

These days, many compilers take this into account, especially for processors that are very picky about misalignment. It is best to search the compiler manual since it can vary between vendors.
 

Re: align in C/C++

the aligned thing is called a compiler directive. This is an instruction executed by you compiler to set the alignment of the variable defined later. This is needed because machines, like ARM, are aligned machines. If addresses are not aligned with data sizes, this will generate a hardware fault. In general, aligned machines provide faster chunks of data access and address generation.
 

Re: align in C/C++

a correction:
----------------------------------------
instead of
addr0 ---
addr1 byte0
addr2 byte1 ---now the processor reads two times.
addr2 ---

first addr0 and 1---get the byte0
second addr1 and 2---get byte1
--------------------------------------------
read as:
addr0 ---
addr1 byte0
addr2 byte1 ---now the processor reads two times.
addr3 ---

first addr0 and 1---get the byte0
second addr2 and 3---get byte1


a compiler meant for that processor takes care of alignment.
no need to specify separately for basic codes.

eg:
gcc -basic c ------takes care of.

for sse codes , special alignment commands are included in header files.

srizbf
22ndjune2010
 

Re: align in C/C++

But i have too seen that
align(1000) unsigned char buffer[10];

what is it and what will be its benefit?

Regards
Sukhdeep Singh
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top