473,765 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it required to comply byte alignment within a function?

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?

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

gamja wrote:
Richard Bos wrote:
"gamja" <as****@gmail.c om> 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 misunderstandin g 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.
Nov 14 '05 #11
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
"gamja" <as****@gmail.c om> 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.
Nov 14 '05 #12
Gordon Burditt wrote:
You CANNOT align local variables.


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

--
pete
Nov 14 '05 #13
On Sat, 14 May 2005 02:13:04 GMT, pete <pf*****@mindsp ring.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #14
Jack Klein wrote:

On Sat, 14 May 2005 02:13:04 GMT, pete <pf*****@mindsp ring.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
Nov 14 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2946
by: Marco | last post by:
Hi I have to read a binary image. Info tell me that it is a binary image without encoding but with RLE. Also i know it is 4 bytes align. What it means ?? How can i read it ?? I red it is for portability whit different microprocessor. is it right ?? How can i perform my 4 bytes in rle decode function when i have to check first byte (length byte) to understand what i have to do with other bytes
4
17693
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word b;
5
1467
by: Richard Harris | last post by:
Hello, Consider this: struct MBD { double x; double y; int i; }
11
3783
by: Taran | last post by:
Hi all, I was wondering how does address alignment to x byte boundary is done. For example, if I say "adjust the requested size to be on a 4-byte boundary" or for that matter 8 byte boundary. How is this adjustment/alignment done? I goolged around alot and wans't able to find how is it done, all it said was what is byte alignment and byte padding.
12
8127
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses raw MMX and SSE power, the starting pointer MUST be starting at a 16 byte memory boundary. In C++ I allocate more memory than needed, and in a second phase I search for the address that starts on a 16 byte boundary. And I then use that new...
5
3914
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are with the same solution. All had "Default" set in the "Struct Member Alignment" entry. After some assembler debugging we found out that a struct member that is a member function pointer in
1
3180
by: Gajendra | last post by:
How does the byte packing and the byte alignment work in VC++ compiler? What is the effect of #pragma pack(n) on the alignment and byte packing for example while using the structur struc double a char b char c } in a 8 byte packing the sizeof structure is 16. Could anyone explain.
13
2357
by: Neo Geshel | last post by:
I have examined about 80+ different upload scripts on the 'net, both in VB and C#, and none seem to do what I need them to do. Perhaps someone here can point me somewhere that Google hasn't reached yet (I have gone all the way to page 50 on Google's results!!). Here are my requirements: • I have a DataGrid. Everything will be done from here. Everything. No exceptions. Everything will also be done in VB, without any code-behind to...
19
4141
by: glchin | last post by:
Does a compiler guarantee that the variable w below is placed on an eight-byte aligned address? void myFunction( long iFreq ) { const double w = two_pi * iFreq; ... ... }
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8830
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7371
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6646
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.