DSPRelated.com
Forums

Getting a wrong sizeof value on a struct

Started by regi...@gmail.com June 6, 2007
Hi
I have a little problem. I am using CodeComposer studio v2.21.
I am working on TI DM642 (DM6415) DSP's
I defind a struct which has 7 bytes :
typedef struct
{
UINT32 s_code; // 4 bytes
UINT8 type_code1; // 1 byte
UINT8 type_code2; // 1 byte
UINT8 type_code3; // 1 byte
}STR_CODE_T;

now, when I am using the command : sizeof(STR_CODE_T);
I get the value of 8, instead of 7.
Can anyone explain why ? I think it something to do with the alignment..??!!
What can I do to get the right value ?
Thank
RegInfo.
Reggie-

> I have a little problem. I am using CodeComposer studio v2.21.
> I am working on TI DM642 (DM6415) DSP's
> I defind a struct which has 7 bytes :
> typedef struct
> {
> UINT32 s_code; // 4 bytes
> UINT8 type_code1; // 1 byte
> UINT8 type_code2; // 1 byte
> UINT8 type_code3; // 1 byte
> }STR_CODE_T;
>
> now, when I am using the command : sizeof(STR_CODE_T);
> I get the value of 8, instead of 7.
> Can anyone explain why ? I think it something to do with the alignment..??!!
> What can I do to get the right value ?

I believe that CCS wants to align data structures to 4-byte boundaries (and possibly
8-byte boundaries) whenever possible. Probably there is an extra byte of "padding"
at the end of your structure... You can check this by closely examining the
generated .map file to see if anything occupies the 8th byte location or not.

-Jeff


Hi,


The proper size is 8. The structure uses a little more spacing to adjust alignment as you guessed. And so the correct size of this structure is precisely 8. The wasted bytes cannot be (normally) used by any other data.


Allocating an array of 10 such structures will have a size of 80, not a size of 70. If you use a size of 7 you will run into plenty of problems. So the good C programming technique is to use the size 8 and forget about the wasted space. The code is more portable, readable, debuggable, upgradable, etc.


If you really need to recover the wasted space, then there are complex tricks that can be used but they will result in slower performance. For example, you can pack the UINT32 into an array of bytes to eliminate the need for alignment. But then you need to unpack the array and convert it to a UINT32 before you can use it. Pakcing or unpacking is constantly needed to write or read that field.


Note: UINT32 enforces the alignment to multiples of 4 (hence why the size 8). This is due to the fact that most DSPs access an integer of 32 bits in one shot from the memory bank, and this integer should start at an address which is a multiple of 4 (it's a wiring issue between DSP data bus and memory data bus).



 




From: r...@gmail.com
Reply-To: r...@gmail.com
To: c...
Subject: [code-comp] Getting a wrong sizeof value on a struct
Date: Tue, 05 Jun 2007 10:37:52 -0400






Hi
I have a little problem. I am using CodeComposer studio v2.21.
I am working on TI DM642 (DM6415) DSP's
I defind a struct which has 7 bytes :
typedef struct
{
UINT32 s_code; // 4 bytes
UINT8 type_code1; // 1 byte
UINT8 type_code2; // 1 byte
UINT8 type_code3; // 1 byte
}STR_CODE_T;

now, when I am using the command : sizeof(STR_CODE_T);
I get the value of 8, instead of 7.
Can anyone explain why ? I think it something to do with the alignment..??!!
What can I do to get the right value ?
Thank
RegInfo.




__._,_.___







">http://www.dsprelated.com/groups/code-comp/1.php



_____________________________________




Computer telephony

Portable computer

Computer job

Portable computer case

Communication and networking












__,_._,___