Problems with sizeof function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RnJhbg==?=

    #1

    Problems with sizeof function

    sHi!

    I have a very strange problem with the behaviour of the sizeof command. I
    have tested it in vc6,vc 2001 and vc 2003. The problem is quite simple.

    When I made a sizeof to the next struct, it returns that the size is 40
    instead of 38. I consider that a short is 2 bytes, float 4 bytes, unsigned
    long 4 byte, unsigned short 2 bytes. If i add all the size of the fields it
    must be 38 but sizeof says that is 40.

    The struct is the next:

    typedef struct
    {
    short est;
    short numbe1;
    float dst;
    float Line;
    unsigned long tmp;
    float tmp1;
    float tmp2;
    unsigned long tck;
    unsigned long tl;
    unsigned short dp;
    unsigned short ldact;
    unsigned short lnc;
    }test;

    If I remove one unsigned short field, the size change to 36, but if made
    size of (unsigned short) it return 2.

    This is the other "crazy" struct:

    typedef struct
    {
    short est;
    short numbe1;
    float dst;
    float Line;
    unsigned long tmp;
    float tmp1;
    float tmp2;
    unsigned long tck;
    unsigned long tl;
    unsigned short dp;
    unsigned short ldact;
    }test;

    I'm getting crazy, any idea??


    Thank you very much for your help.



    The structure is the next:


  • Cholo Lennon

    #2
    Re: Problems with sizeof function

    This is a common question. Take a look to:


    izeof#72c336cb6 77db72c

    Regards

    --
    Cholo Lennon
    Bs.As.
    ARG


    "Fran" <Fran@discussio ns.microsoft.co mwrote in message news:193B0BFA-3231-43CE-9DB6-3ABC9D589CB3@mi crosoft.com...
    sHi!
    >
    I have a very strange problem with the behaviour of the sizeof command. I
    have tested it in vc6,vc 2001 and vc 2003. The problem is quite simple.
    >
    When I made a sizeof to the next struct, it returns that the size is 40
    instead of 38. I consider that a short is 2 bytes, float 4 bytes, unsigned
    long 4 byte, unsigned short 2 bytes. If i add all the size of the fields it
    must be 38 but sizeof says that is 40.
    >
    The struct is the next:
    >
    typedef struct
    {
    short est;
    short numbe1;
    float dst;
    float Line;
    unsigned long tmp;
    float tmp1;
    float tmp2;
    unsigned long tck;
    unsigned long tl;
    unsigned short dp;
    unsigned short ldact;
    unsigned short lnc;
    }test;
    >
    If I remove one unsigned short field, the size change to 36, but if made
    size of (unsigned short) it return 2.
    >
    This is the other "crazy" struct:
    >
    typedef struct
    {
    short est;
    short numbe1;
    float dst;
    float Line;
    unsigned long tmp;
    float tmp1;
    float tmp2;
    unsigned long tck;
    unsigned long tl;
    unsigned short dp;
    unsigned short ldact;
    }test;
    >
    I'm getting crazy, any idea??
    >
    >
    Thank you very much for your help.
    >
    >
    >
    The structure is the next:
    >
    >



    Comment

    • Mark Salsbery [MVP]

      #3
      Re: Problems with sizeof function

      You may want to read the "Remarks" section here as well:

      http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx

      Mark

      --
      Mark Salsbery
      Microsoft MVP - Visual C++


      "Fran" <Fran@discussio ns.microsoft.co mwrote in message
      news:193B0BFA-3231-43CE-9DB6-3ABC9D589CB3@mi crosoft.com...
      sHi!
      >
      I have a very strange problem with the behaviour of the sizeof command. I
      have tested it in vc6,vc 2001 and vc 2003. The problem is quite simple.
      >
      When I made a sizeof to the next struct, it returns that the size is 40
      instead of 38. I consider that a short is 2 bytes, float 4 bytes, unsigned
      long 4 byte, unsigned short 2 bytes. If i add all the size of the fields
      it
      must be 38 but sizeof says that is 40.
      >
      The struct is the next:
      >
      typedef struct
      {
      short est;
      short numbe1;
      float dst;
      float Line;
      unsigned long tmp;
      float tmp1;
      float tmp2;
      unsigned long tck;
      unsigned long tl;
      unsigned short dp;
      unsigned short ldact;
      unsigned short lnc;
      }test;
      >
      If I remove one unsigned short field, the size change to 36, but if made
      size of (unsigned short) it return 2.
      >
      This is the other "crazy" struct:
      >
      typedef struct
      {
      short est;
      short numbe1;
      float dst;
      float Line;
      unsigned long tmp;
      float tmp1;
      float tmp2;
      unsigned long tck;
      unsigned long tl;
      unsigned short dp;
      unsigned short ldact;
      }test;
      >
      I'm getting crazy, any idea??
      >
      >
      Thank you very much for your help.
      >
      >
      >
      The structure is the next:
      >
      >

      Comment

      Working...