Solved

Why are we reading incorrect length ?

Posted on 2013-11-05
7
270 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Title # Comments Views Activity
I could not build boost code, 10 95
How to read a c++ line with strings and variable numbers of elements 6 40
Link failure 16 34
boost::uuid crashes 17 12
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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.

838 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