Solved

Why are we reading incorrect length ?

Posted on 2013-11-05
7
264 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 82

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now