Solved

Why are we reading incorrect length ?

Posted on 2013-11-05
7
268 Views
Last Modified: 2013-11-05
Our embedded target board is ARM7 based.

struct SOME_STRUCT
{
  unsigned short objectId;
  unsigned long  length;
};
We read sizeof(SOME_STRUCT) from buffer.
sizeof(SOME_STRUCT) - Not sure why the lenght is Eight bytes when the structure contains a short and a long.

The data in the buffer is as follows:

01 00 00 08 00 00 00 00 00 00

We thought we were reading six bytes from the buffer but we're reading eight bytes from it.
objectId is 0x0001
length is 0x00000000

We're using Keil compiler Armcc.Exe V4.1.0.894

It looks like we have some kind of alignment issue? Does this compiler only read from word aligned addresses ? What is the root cause of the problem ? Do we need some kind of PACK or packed keyword so compiler reads six bytes from the buffer instead of eight ?


Here is the link to Keil's compiler:      http://www.keil.com/support/man_arm.htm
0
Comment
Question by:naseeam
  • 4
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39625749
Your compiler is padding the structure's size to align it on a word boundary, that's why the size is different. This is quite a common behaviour. See http://www.keil.com/support/man/docs/armcc/armcc_cjajddhb.htm ("Types of data alignment") and the pages linked from there, it says that Keil supports '#pragma pack(n)'
0
 
LVL 1

Author Comment

by:naseeam
ID: 39625952
I believe with packed data all accesses must contain lots of extra processor instructions.  Is there a better solution ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39625965
You can limit that to a single struct if you are concerned about that, see http://www.keil.com/support/man/docs/ARMCCREF/ARMCCREF_CJAFEEDG.htm ("#pragma pack(n)")
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:naseeam
ID: 39625986
ok I see.

There is a class after the struct.  I only want to apply pack to the struct.  Will it also get applied to the class ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39625999
If you want to ensure it only affexts one single struct, do that like

#pragma pack(2) 
struct SOME_STRUCT
{
  unsigned short objectId;
  unsigned long  length;
};
#pragma pack(4)

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39626214
It seems to me that if you put the 'long' first and the 'short' second then they both would align to word boundaries.  Putting the 'long' second puts 2 bytes in the first word and 2 bytes in the second word which crosses the word boundary.  Just a thought.
struct SOME_STRUCT
{
  unsigned long  length;
  unsigned short objectId;
};

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39626221
Um, no, the order won't matter. It's always 6 bytes.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question