This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem using sizeof() with element of struct

Hi all! I am experiencing the following problem:

sizeof(EDA.stPARAM) returns 0x80 with CA.exe V2.41 which in my opinion is the correct result, but with CA.exe V2.50a it returns 0x82. Can somebody explain this behaviour to me? Code sample is included below:

file eda_st.inc:

struct
{

byte ubFree_0000h[16] ;/* 000h-00Fh: */

byte ubFree_0010h[16] ;/* 010h-01Fh: */

byte ubFree_0020h[16] ;/* 020h-02Fh: */

byte ubFree_0030h[16] ;/* 030h-03Fh: */

byte ubFree_0040h[16] ;/* 040h-04Fh: */

byte ubFree_0050h[16] ;/* 050h-05Fh: */

byte ubFree_0060h[16] ;/* 060h-06Fh: */

byte ubFree_0070h[16] ;/* 070h-07Fh: */

}stPARAM;

struct
{

byte VER_HWVers ;/* 000h: */

byte ubFree_0000h[127] ;/* 001h-07fh: */

}stABGLEICH;

----------------------
file eda_usr.h:

typedef struct
{

#include "eda_st.inc" // struct mit Parametern

} EDA_sttyDAT;

extern EDA_sttyDAT EDA;

----------------------
file eda_usr.c:

#define TAR_RAMCOPY 0x40001000

EDA_sttyDAT EDA __at(TAR_RAMCOPY);// RAM-Kopie

Parents
  • Maybe the compiler want to add an extra byte of empty data at end of struct, to make sure that it is allowed to do a 16-bit access to the last byte without overflowing into unknown data space.

    But since 1 bytes of overflow protection makes the struct incorrectly aligned, the compiler then has to add more padding.

    Never depend on a specific size of a data structure.

    By the way, why are you plaing with include files when defining your struct? It doesn't help readability.

Reply
  • Maybe the compiler want to add an extra byte of empty data at end of struct, to make sure that it is allowed to do a 16-bit access to the last byte without overflowing into unknown data space.

    But since 1 bytes of overflow protection makes the struct incorrectly aligned, the compiler then has to add more padding.

    Never depend on a specific size of a data structure.

    By the way, why are you plaing with include files when defining your struct? It doesn't help readability.

Children