all groups > visual c > november 2007 >
You're in the

visual c

group:

Problems with sizeof function


Problems with sizeof function Fran
11/2/2007 5:41:01 AM
visual c:
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:

Re: Problems with sizeof function Cholo Lennon
11/2/2007 10:18:41 AM
This is a common question. Take a look to:

http://groups.google.com/group/microsoft.public.vc.language/browse_frm/thread/8e7400441b97eb1e/72c336cb677db72c?lnk=gst&q=struct+s
izeof#72c336cb677db72c

Regards

--
Cholo Lennon
Bs.As.
ARG


[quoted text, click to view]



Re: Problems with sizeof function Mark Salsbery [MVP]
11/2/2007 11:45:11 AM
You may want to read the "Remarks" section here as well:

http://msdn2.microsoft.com/en-us/library/83ythb65(VS.80).aspx

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


[quoted text, click to view]
AddThis Social Bookmark Button