473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
Nov 2 '07 #1
2 1364
This is a common question. Take a look to:

http://groups.google.com/group/micro...gst&q=struct+s
izeof#72c336cb6 77db72c

Regards

--
Cholo Lennon
Bs.As.
ARG
"Fran" <Fr**@discussio ns.microsoft.co mwrote in message news:19******** *************** ***********@mic rosoft.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:


Nov 2 '07 #2
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" <Fr**@discussio ns.microsoft.co mwrote in message
news:19******** *************** ***********@mic rosoft.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:

Nov 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2518
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now when my function returns, the values stored in the char* are some garbage values (perhaps because I didn't allocate any memory for them).. but even if...
3
2290
by: kimimaro | last post by:
hi below is my save function that is used to placed data from the C program to a text file for future usage. void save() { FILE *save; int i = 0; save=fopen("employeerecord.txt", "a+");
14
1832
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works, but I still get some errors when i try to compile it: stack.c: In function 'reverseFile'
2
3258
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs...
9
2187
by: Sheldon | last post by:
Good day Everyone, I am a still very new at learning C and I have thrown myself in the deep end. I started with a simple program and kept widening the scope. This has taught me many things about C and some, I must admit, have not really sunk in yet. Still, I push on. Now I am taken a library of C programs that were designed to read HDF...
7
8693
by: roguefeebo | last post by:
I'm very new to programming so be gentle, :( Essentially what I would like to do is to have a single function to be able to create a dynamic array of pointers to a struct so that I can have a modular menu system up and running. My menus will have several varying numbers of options. This is the first time I've worked with something like this...
2
2950
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not that much, but when there is more data to write, after a while I get data errors when reading back the data. Error in main: couldn't read stat...
7
1837
by: lancer6238 | last post by:
Hi, I'm writing a program that separates a set of integers into groups and then quicksort each group individually. However, I'm having problems with my realloc() function. (Pardon me if the indentation is weird. There's no preview button here and I can't tell if my indentation is correct.) #include <stdio.h> #include <stdlib.h>
409
10825
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are objects that are more than 4GB long. The problem is, when you have in thousands of places
6
2249
by: msb_6 | last post by:
Currently I have a PHP extension thats all written and compiles under windows, but the PC I'm going to end up putting it on is running Ubuntu 8.04 (g++ 4.2.3). I've delved into PHP documentation and ended up with a simple config.m4 file and the process to compile. However when I try to compile I get loads of errors that I didn't get when...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.