|
|
View previous topic :: View next topic |
Author |
Message |
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
Big Problem with Packed struct in Version 5.007 and 5.012 |
Posted: Sat Oct 12, 2013 3:18 pm |
|
|
I make a example I compile this code with 3 different version and all output are different:
Quote: |
V 4.141
Test...
0001000050000000C359000000027FD6AB5B2308547697092E14150B00000000DB00008F5C0500004A57F40200008F5C020700A4000C020D04190034000414 5C8F0000 DB0000008BA0E21BEBD62FC0F623C169000000
StSize 316
256
80
FW:15-0B LTC:3674210304 Monto:1552875520 DF:5 TxID:4099361280 C#:2 $C:1552875520 ELG:2...
V 5.007
Test...
0001000050000000C359000000027FD6AB5B2308547697092E14150B00000000DB00008F5C0500004A57F40200008F5C020700A4000C020D041900340004145C8F0000DB0000008BA0E21BEBD62FC0F623C169000000
StSize 318
256
80
FW:15-0B LTC:14352384 Monto:89952000 DF:0 TxID:49567562 C#:0 $C:117595279 ELG:0...
V 5.012
Test...
0001000050000000C359000000027FD6AB5B2308547697092E14150B00000000DB00008F5C0500004A57F40200008F5C020700A4000C020D041900340004145C8F0000DB0000008BA0E21BEBD62FC0F623C169000000
StSize 316
0
0
FW:00-00 LTC:0 Monto:0 DF:0 TxID:0 C#:2 $C:1552875520 ELG:254... |
The 4.141 work ok, the news version are wrong.
This is my code, and I use a PIC24H
Code: | void main()
{
UINT8 uiPtr;
UCHAR *sPtr;
struct STRUCT_PACKED
{
UINT32 udPOSID;
UINT16 ulDataLGLen;
UCHAR sPOSLGSAMId[ 20 ];
UINT8 uiFWVersion;
UINT8 uiFWRelease;
UINT8 uiNULL;
UINT32 udLTC;
UINT32 udMonto;
UINT16 ulDF;
UINT32 udTransactionID;
UINT8 uiCommandCounter;
UINT32 udImporteCargado;
UINT8 uiEstadoLG;
UCHAR sAPDU[ 267 ];
} stDataLG2;
memcpy( &stDataLG2, "\x00\x01\x00\x00\x50\x00\x00\x00\xC3\x59\x00\x00\x00\x02\x7F\xD6\xAB\x5B\x23\x08\x54\x76\x97\x09\x2E\x14\x15\x0B\x00\x00\x00\x00\xDB\x00\x00\x8F\x5C\x05\x00\x00\x4A\x57\xF4\x02\x00\x00\x8F\x5C\x02\x07\x00\xA4\x00\x0C\x02\x0D\x04\x19\x00\x34\x00\x04\x14\x5C\x8F\x00\x00\xDB\x00\x00\x00\x8B\xA0\xE2\x1B\xEB\xD6\x2F\xC0\xF6\x23\xC1\x69\x00\x00\x00", 86 );
sPtr = &stDataLG2;
fprintf( SERIAL_DBG, "Test...\n\r" );
for ( uiPtr = 0 ; uiPtr < 86 ; uiPtr++ )
fprintf( SERIAL_DBG, "%02X", sPtr[ uiPtr ] );
fprintf( SERIAL_DBG, "\n\r" );
fprintf( SERIAL_DBG, "StSize %lu\n\r", sizeof( stDataLG2 ) );
fprintf( SERIAL_DBG, "%lu\n\r", stDataLG2.udPOSID );
fprintf( SERIAL_DBG, "%lu\n\r", stDataLG2.ulDataLGLen );
fprintf( SERIAL_DBG, "FW:%02X-%02X LTC:%lu Monto:%lu DF:%lu TxID:%lu C#:%lu $C:%lu ELG:%u", stDataLG2.uiFWVersion, stDataLG2.uiFWRelease, stDataLG2.udLTC, stDataLG2.udMonto, stDataLG2.ulDF, stDataLG2.udTransactionID, stDataLG2.uiCommandCounter, stDataLG2.udImporteCargado, stDataLG2.uiEstadoLG );
fprintf( SERIAL_DBG, "...\n\r" );
while( TRUE )
restart_wdt( );
__PROGRAM_MEMORY_END__:
;
} |
The STRUCT_PACKED is a define for __attribute__((packed)) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sun Oct 13, 2013 1:00 am |
|
|
The 16bit PIC's _require_ int32's and int16's, to be word aligned.
Your layout:
UINT32 udTransactionID;
UINT8 uiCommandCounter;
UINT32 udImporteCargado;
UINT8 uiEstadoLG;
will give a problem because of this.
If you search on the forum, you will find this being discussed.
If you need the alignment (to allow the data to be used elsewhere), use:
<http://www.ccsinfo.com/forum/viewtopic.php?t=44569&highlight=word+align>
You are _assuming_ that data in a structure is placed sequentially in memory. There is no guarantee of this, except for 'entities' like arrays etc.. The optimiser either has to add an extra padding byte when the word does not align, or relocate entities to get the required alignment. Why not just initialise the structure in the declaration?. If you use the standard initialisation format, then the compiler knows which entry corresponds to which element, and moves the entries as needed. As a 'block' of data in memory, moved with memcpy, there is no such guarantee.
You'd get the same problem if you tried to do a memory move like this to the array on a PC, unless you forced this to be aligned. Beware the caveat though if you start trying to use pointers to the array...
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|