Solved

Why are we reading incorrect length ?

Posted on 2013-11-05
7
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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