Is it required to comply byte alignment within a function?

  • Follow


Hi all.
This is my first post on this group. Nice to meet you, cool guys~!

I'm on system programming on various embedded systems and understand
very well the byte alignment issues. When I write C code, especially
design a structure, I pay attention to the order and size of member
variables. Because, my boss always says that all variables should be
aligned by 4byte boundary, if not a data abort will be occurred on a
specific machine such like ARM.

However, I've never seen such situation. Because, my compiler
automatically aligns the boundary and gives padding properly between
each variable in the structure, if needed. Of course, pack option of
the compiler may change its behavior and this assumption may be break
easily. So, I agree with my boss.

The problem I want to ask is that he wants me to align all local
variables even in a function. For example, see the following code.

Case #1 - THIS IS NOT ALLOWED!!
int foo1()
{
    char a;
    int b;
    short c;
    ...
}

Case #2 - THIS IS OK. GOOD.
int foo2()
{
    char a;
    char dummy[3];
    int b;
    short c;
    ...
}

I think this is bothersome and even ridiculous.

How do you think about?
Is the case #1 can make a data abort or bus error?

0
Reply assert (3) 5/13/2005 8:49:04 AM

"gamja" <assert@gmail.com> wrote:

> The problem I want to ask is that he wants me to align all local
> variables even in a function. For example, see the following code.
> 
> Case #1 - THIS IS NOT ALLOWED!!
> int foo1()
> {
>     char a;
>     int b;
>     short c;
>     ...
> }
> 
> Case #2 - THIS IS OK. GOOD.
> int foo2()
> {
>     char a;
>     char dummy[3];
>     int b;
>     short c;
>     ...
> }
> 
> I think this is bothersome and even ridiculous.
> 
> How do you think about?

The man is a fool, and that for more reasons than one.

> Is the case #1 can make a data abort or bus error?

If it does, you're not using a C compiler, but a bodge job.

Richard
0
Reply rlb (4118) 5/13/2005 9:05:40 AM


May I know why it is? Hmm.. I need more information somewhat technical
to persuade my boss.

0
Reply assert (3) 5/13/2005 9:15:46 AM

gamja <assert@gmail.com> wrote:
> I'm on system programming on various embedded systems and understand
> very well the byte alignment issues. When I write C code, especially
> design a structure, I pay attention to the order and size of member
> variables. Because, my boss always says that all variables should be
> aligned by 4byte boundary, if not a data abort will be occurred on a
> specific machine such like ARM.

> However, I've never seen such situation. Because, my compiler
> automatically aligns the boundary and gives padding properly between
> each variable in the structure, if needed. Of course, pack option of
> the compiler may change its behavior and this assumption may be break
> easily. So, I agree with my boss.

> The problem I want to ask is that he wants me to align all local
> variables even in a function. For example, see the following code.

> Case #1 - THIS IS NOT ALLOWED!!
> int foo1()
> {
>     char a;
>     int b;
>     short c;
>     ...
> }

> Case #2 - THIS IS OK. GOOD.
> int foo2()
> {
>     char a;
>     char dummy[3];
>     int b;
>     short c;
>     ...
> }

> I think this is bothersome and even ridiculous.

It is. If your C compiler won't produce code that puts the variables
at addresses with proper alignment all by itself then that compiler
is horribly broken. Your case #1 is perfectly legal and each and
every compiler that has any right to call itself a C compiler will
put in the necessary padding all by itself or re-arrange the order
of the variables - there's no requirement that 'a' comes at lower
address than 'b' just because you defined 'a' before 'b'. So, on
some architectures you may end up with variables reordered to e.g.
'b', 'c', 'a'.

If what your boss claims would be true it would be nearly impossible
to write portable programs. There are lots of architectures with
different alignment requirements (e.g. for some types and machines
you may have to align not on 4-byte boundaries but on 8-byte or
maybe even 16-byte boundaries). And if you write programs that
are supposed to be independent of a special architecture then you
won't be able to know in advance how much padding is going to be
needed. This is only known to the compiler on the machine your
program finally is going to be compiled on and _it_ has to take
care of this problem.

Of course, some compilers have an (non-standard) option to overrule
this behaviour (but only, as far as I have seen, for structures).
Only if you insist on using that optionit becomes your responsibility
to take care of alignment issues, but then you asked for it and you
get given enough rope to hang yourself. But as long as you don't use
this option also all member variables of structures will always be
aligned properly by the compiler.

                                  Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de
0
Reply Jens.Toerring (807) 5/13/2005 10:27:14 AM

gamja wrote:
> 
> Hi all.
> This is my first post on this group. Nice to meet you, cool guys~!
> 
> I'm on system programming on various embedded systems and understand
> very well the byte alignment issues. When I write C code, especially
> design a structure, I pay attention to the order and size of member
> variables. Because, my boss always says that all variables should be
> aligned by 4byte boundary, if not a data abort will be occurred on a
> specific machine such like ARM.
> 
> However, I've never seen such situation. Because, my compiler
> automatically aligns the boundary and gives padding properly between
> each variable in the structure, if needed.

Just like any C compiler.

> Of course, pack option of
> the compiler may change its behavior and this assumption may be break
> easily. So, I agree with my boss.
> 
> The problem I want to ask is that he wants me to align all local
> variables even in a function. For example, see the following code.
> 
> Case #1 - THIS IS NOT ALLOWED!!
> int foo1()
> {
>     char a;
>     int b;
>     short c;
>     ...
> }
> 
> Case #2 - THIS IS OK. GOOD.
> int foo2()
> {
>     char a;
>     char dummy[3];
>     int b;
>     short c;
>     ...
> }
> 
> I think this is bothersome and even ridiculous.
> 
> How do you think about?

There's problems in the logic as stated.
If case #2 does what you want, 
then it's merely a matter of coincidence.
There's nothing in the rules of C which implies that 
if two automatic objects are defined consecutively in source code,
that they then should have consecutive addresses.
There's also nothing in your boss' stated philosophy which
let's you assume that char a is properly aligned to begin with.

> Is the case #1 can make a data abort or bus error?

Only if there is problem with the compiler.
Making sure that automatic objects are properly aligned
for their own access, 
is part of what a C compiler is supposed to do.

-- 
pete
0
Reply pfiland (6613) 5/13/2005 11:00:04 AM

Hi,

Absolutely agree with everyone....order of variable declaration doesn't
have anything to do with order of those variables in memory after
compilation.

-vs_p...

0
Reply vs_praveen (35) 5/13/2005 12:00:23 PM

gamja wrote:
> 
> May I know why it is? Hmm.. I need more information somewhat
> technical to persuade my boss.

Why what is?  You need to quote adequate context so every article
stands by itself.  If you must use the foul and broken google
interface to usenet see below.  Or better, get a real newsreader.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


0
Reply cbfalconer (19183) 5/13/2005 1:34:16 PM

Achintya wrote:
> 
> Absolutely agree with everyone....order of variable declaration
> doesn't have anything to do with order of those variables in
> memory after compilation.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


0
Reply cbfalconer (19183) 5/13/2005 1:34:18 PM

>I'm on system programming on various embedded systems and understand
>very well the byte alignment issues. When I write C code, especially
>design a structure, I pay attention to the order and size of member
>variables. Because, my boss always says that all variables should be
>aligned by 4byte boundary, if not a data abort will be occurred on a
>specific machine such like ARM.

All variables will be aligned properly by the C compiler to appropriate
alignment for the target machine, unless you're doing things with
pointers like interpreting a character buffer as a struct when it
wasn't declared that way.

C coders cannot align variables themselves, especially not auto
variables.

That assumption of a 4-byte boundary is not portable.  Some machines
may require an 8-byte boundary, some may require a 2-byte boundary,
and there is no standards reason why a machine couldn't require a
7-byte boundary, although I doubt you'll ever see one commercially
available.  Also, chars don't require alignment to more than 1 byte,
and they are by definition 1 byte in size (even if that byte is 37
bits long).

>The problem I want to ask is that he wants me to align all local
>variables even in a function. For example, see the following code.

You CANNOT align local variables.

>Case #1 - THIS IS NOT ALLOWED!!
>int foo1()
>{
>    char a;
>    int b;
>    short c;
>    ...
>}
>
>Case #2 - THIS IS OK. GOOD.
>int foo2()
>{
>    char a;
>    char dummy[3];
>    int b;
>    short c;

*NO* that is *NOT* good, since auto variables are ordered alphabetically
by *NAME*, or by the NAME spelled backwards, or by the NAME translated
into Pig Latin, and 'dummy' comes after 'c'.  (Well, that's just
as stupid as the assumption that auto variables are stored in order
of declaration.)

						Gordon L. Burditt
0
Reply gordonb.djmj2 (1) 5/13/2005 4:01:19 PM

gamja wrote:

> Hi all.
> This is my first post on this group. Nice to meet you, cool guys~!
> 
> I'm on system programming on various embedded systems and understand
> very well the byte alignment issues. When I write C code, especially
> design a structure, I pay attention to the order and size of member
> variables. Because, my boss always says that all variables should be
> aligned by 4byte boundary, if not a data abort will be occurred on a
> specific machine such like ARM.
> 
> However, I've never seen such situation. Because, my compiler
> automatically aligns the boundary and gives padding properly between
> each variable in the structure, if needed. Of course, pack option of
> the compiler may change its behavior and this assumption may be break
> easily. So, I agree with my boss.
> 
> The problem I want to ask is that he wants me to align all local
> variables even in a function. For example, see the following code.
> 
> Case #1 - THIS IS NOT ALLOWED!!
> int foo1()
> {
>     char a;
>     int b;
>     short c;
>     ...
> }
> 
> Case #2 - THIS IS OK. GOOD.
> int foo2()
> {
>     char a;
>     char dummy[3];
>     int b;
>     short c;
>     ...
> }
> 
> I think this is bothersome and even ridiculous.
> 
> How do you think about?
> Is the case #1 can make a data abort or bus error?
> 
This is malarky unless you force the compiler to to
do something bad (via pragmas, such as PACK).

The compiler will automatically add padding between
members of a structure or union to make accessing
the data more efficient.  This poses a problem when
trying to use structures to model real-world data.
The compiler will place variables at addresses for
improved efficiency unless the programmer tells
the compiler otherwise.

-- 
Thomas Matthews

C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.comeaucomputing.com/learn/faq/
Other sites:
     http://www.josuttis.com  -- C++ STL Library book
     http://www.sgi.com/tech/stl -- Standard Template Library
0
Reply Thomas_MatthewsSpamBotsSuck (408) 5/13/2005 4:33:48 PM

I've re-instated the context so that people can see what is actually 
being talked about.

gamja wrote:
 > Richard Bos wrote:
>>"gamja" <assert@gmail.com> wrote:
>>> The problem I want to ask is that he wants me to align all local
>>> variables even in a function. For example, see the following code.
>>> 
>>> Case #1 - THIS IS NOT ALLOWED!!
>>> int foo1()
>>> {
>>>     char a;
>>>     int b;
>>>     short c;
>>>     ...
>>> }

If in assembler you did something like
a .space 1   # allocate 1 byte
b .space 4   # allocate 4 bytes
c .space 2   # allocate 2 bytes

on a system where alignment for integer access mattered then tried to 
read b as a 4 byte integer then yes, this could lead to problems.

The wonder of high level languages is they *compiler* sorts out this 
stuff for you except where you deliberately trick it.

>>> Case #2 - THIS IS OK. GOOD.
>>> int foo2()
>>> {
>>>     char a;
>>>     char dummy[3];
>>>     int b;
>>>     short c;
>>>     ...
>>> }
>>> 
>>> I think this is bothersome and even ridiculous.
>>> 
>>> How do you think about?
>
>The man is a fool, and that for more reasons than one.

The man is a fool probably thinking of assembler and applying the same 
rules to C where they make no sense.

Or the OP is misunderstanding his boss.

>>> Is the case #1 can make a data abort or bus error?
>>
>>If it does, you're not using a C compiler, but a bodge job.
 >
> May I know why it is? Hmm.. I need more information somewhat technical
> to persuade my boss.

The language definition says that if you declare a variable of a given 
type you can use it as that type. Anything else would not make sense.

You could tell you boss to read *any* C text book. If that won't do, 
tell him/her to by a copy of the C standard.
-- 
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
0
Reply spam331 (4024) 5/13/2005 4:58:55 PM

In article <1115974144.280550.197820@f14g2000cwb.googlegroups.com>,
 "gamja" <assert@gmail.com> wrote:

> Hi all.
> This is my first post on this group. Nice to meet you, cool guys~!
> 
> I'm on system programming on various embedded systems and understand
> very well the byte alignment issues. When I write C code, especially
> design a structure, I pay attention to the order and size of member
> variables. Because, my boss always says that all variables should be
> aligned by 4byte boundary, if not a data abort will be occurred on a
> specific machine such like ARM.
> 
> However, I've never seen such situation. Because, my compiler
> automatically aligns the boundary and gives padding properly between
> each variable in the structure, if needed. Of course, pack option of
> the compiler may change its behavior and this assumption may be break
> easily. So, I agree with my boss.
> 
> The problem I want to ask is that he wants me to align all local
> variables even in a function. For example, see the following code.
> 
> Case #1 - THIS IS NOT ALLOWED!!
> int foo1()
> {
>     char a;
>     int b;
>     short c;
>     ...
> }
> 
> Case #2 - THIS IS OK. GOOD.
> int foo2()
> {
>     char a;
>     char dummy[3];
>     int b;
>     short c;
>     ...
> }
> 
> I think this is bothersome and even ridiculous.

I'd hate to work for your boss. 
I'd hate even more to be the person who is your boss.
0
Reply christian.bau (880) 5/13/2005 9:44:14 PM

Gordon Burditt wrote:
 
> You CANNOT align local variables.

If they are union members, then you can.
I used to be a shop steward.

-- 
pete
0
Reply pfiland (6613) 5/14/2005 2:13:04 AM

On Sat, 14 May 2005 02:13:04 GMT, pete <pfiland@mindspring.com> wrote
in comp.lang.c:

> Gordon Burditt wrote:
>  
> > You CANNOT align local variables.
> 
> If they are union members, then you can.
> I used to be a shop steward.

Yes indeed, you can align them to the beginning of the union.  But, of
course, you can't align the beginning of the union.

I used to eat shop stewards for breakfast.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
0
Reply jackklein (3932) 5/14/2005 10:44:36 PM

Jack Klein wrote:
> 
> On Sat, 14 May 2005 02:13:04 GMT, pete <pfiland@mindspring.com> wrote
> in comp.lang.c:
> 
> > Gordon Burditt wrote:
> >
> > > You CANNOT align local variables.
> >
> > If they are union members, then you can.
> > I used to be a shop steward.
> 
> Yes indeed, you can align them to the beginning of the union.  But, of
> course, you can't align the beginning of the union.

You can align the union with every type that is a member of the union.
If you want the union aligned for type int, just give it an int member.

-- 
pete
0
Reply pfiland (6613) 5/15/2005 9:46:57 AM

14 Replies
36 Views

(page loaded in 0.196 seconds)


Reply: