[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Why are we reading incorrect length ?

Posted on 2013-11-05
7
Medium Priority
?
289 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 2000 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 84

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

649 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