Struct members memory order

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Struct members memory order

1,322 Views
S_B_D
Contributor I

Hi,

 

Is there a way to tell the linker to place struct members in memory in the order they where defined in the code? This will place the struct members in consecutive order as written in the struct definition.

The reason for this is to use the memcpy function to copy an entire memory segment ("as is") into the struct.

 

Thanks,

S.B.D

Labels (1)
0 Kudos
3 Replies

311 Views
CrasyCat
Specialist III

Hello

 

As far as I could check structure fields are allocated in the sequence they are defined.

- Which CPU are you targeting (HC08, HC12, Coldfire, ..)
- Which version of CodeWarrior are you using?
To retrieve that info:
- Start CodeWarrior
- Select Help -> About Freescale CodeWarrior
- Click on "Install Products"
- CodeWarrior version used is displayed on top in the Installed Products dialog.

 

Additionally how is the structure defined?

CrasyCat

0 Kudos

311 Views
S_B_D
Contributor I

Thanks for the reply CrasyCat.

 

I'm working on the MC9S08JM32 on Code-Warrior 5.9.0 build 2830.

 

As you will see the structure is not aligned:

 

typedef struct{
                              
unsigned char  Misc;                 

unsigned int   ConnectedSensors[8];    

unsigned char   Timer1Prescaler;        
unsigned int   Timer1AuxCounter;       
unsigned int   Timer1Modulo;            

unsigned char   NumberOfSamplesInPacket;
unsigned int   NumberOfSamplesInPacketToPc;
unsigned char  PcRate;               
unsigned int   TriggerLevelHigh;        
unsigned int    TriggerLevelLow;           

unsigned char   TriggerType_Input;      

unsigned int   Calibration[8];     

unsigned char   Timing[4];        
}Status;
extern Status STATUS, *STATUSptr;

 

S.B.D

0 Kudos

311 Views
CompilerGuru
NXP Employee
NXP Employee

ANSI C requires that struct members are allocated in the declaration order, why do you think this is not happening here?

For copying structs, memcpy should work however just assigning structs is even easier.

Also not sure what your comment about alignment is about, alignment is a different issue than the order which fields are allocated. For a 8 bit chip as a S08, there is usually no alignment padding added by the compiler. In general, alignment is compiler specific and portable code should not rely on (the absence of) alignment.

 

Daniel

0 Kudos