473,698 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comparing sizes of two structures

Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?

Mar 7 '07 #1
20 1899
su************* *@yahoo.com, India wrote:
Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?
Either, it depends on the types and the implementation.

--
Ian Collins.
Mar 7 '07 #2
su************* *@yahoo.com, India wrote:
Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?
It depends on the compiler and the characteristics , (mainly alignment
requirements), of your types.

Mar 7 '07 #3
On 7 Mar 2007 00:16:50 -0800, "su************ **@yahoo.com, India"
<su************ **@yahoo.comwro te:
>Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?
They could be the same or they could be different. It depends on the
exact types of TYPE* and on the compiler. Compilers can add padding in
order to align fields on their natural boundaries, and there is no
guarantee that one compiler will pack fields the same as another
compiler, especially so when the compilers target different platforms.

Try compiling and running the following:

#include <stddef.h>
#include <stdio.h>

typedef char TYPE1;
typedef double TYPE2;
typedef int TYPE3;
typedef short TYPE4;
typedef long TYPE5;

struct s1
{
TYPE1 d1;
TYPE2 d2;
TYPE3 d3;
TYPE4 d4;
TYPE5 d5;
} var_one;

struct s2
{
TYPE2 d1;
TYPE5 d2;
TYPE1 d3;
TYPE3 d4;
TYPE4 d5;
} var_two;

int main(void)
{
printf("s1 offsets:\n");
printf("offseto f(d1) = %d\n", (int)offsetof(s truct s1, d1));
printf("offseto f(d2) = %d\n", (int)offsetof(s truct s1, d2));
printf("offseto f(d3) = %d\n", (int)offsetof(s truct s1, d3));
printf("offseto f(d4) = %d\n", (int)offsetof(s truct s1, d4));
printf("offseto f(d5) = %d\n", (int)offsetof(s truct s1, d5));

printf("\n");
printf("s2 offsets:\n");
printf("offseto f(d1) = %d\n", (int)offsetof(s truct s2, d1));
printf("offseto f(d2) = %d\n", (int)offsetof(s truct s2, d2));
printf("offseto f(d3) = %d\n", (int)offsetof(s truct s2, d3));
printf("offseto f(d4) = %d\n", (int)offsetof(s truct s2, d4));
printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));

return 0;
}

My conforming C implementation outputs:

s1 offsets:
offsetof(d1) = 0
offsetof(d2) = 8
offsetof(d3) = 16
offsetof(d4) = 20
offsetof(d5) = 24

s2 offsets:
offsetof(d1) = 0
offsetof(d2) = 8
offsetof(d3) = 12
offsetof(d4) = 16
offsetof(d5) = 20
Press any key to continue

Regards
--
jay
Mar 7 '07 #4
su************* *@yahoo.com, India <su************ **@yahoo.comwro te:
>How do the sizes of var_one and var_two compare - will they be the
same or different ?
Like others have said, it depends, but I'd answer in general "probably
different".

In the last big project I worked on, we'd allocate tons of structures at
run time, and the platform was relatively limited in memory. As part of
our optimizations, we went back and rearranged the fields in the structs
by hand so the compiler would put less padding in.

We realized significant memory savings.

-Beej

Mar 7 '07 #5
su************* *@yahoo.com, India wrote:
Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?
Yes, one or the other.

Why do you care?

--
Chris "electric hedgehog" Dollin
"Possibly you're not recalling some of his previous plans." Zoe, /Firefly/

Mar 7 '07 #6
jaysome wrote:
On 7 Mar 2007 00:16:50 -0800, "su************ **@yahoo.com, India"
<su************ **@yahoo.comwro te:
Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?
[ ... ]
Try compiling and running the following:

#include <stddef.h>
#include <stdio.h>

typedef char TYPE1;
typedef double TYPE2;
typedef int TYPE3;
typedef short TYPE4;
typedef long TYPE5;

struct s1
{
TYPE1 d1;
TYPE2 d2;
TYPE3 d3;
TYPE4 d4;
TYPE5 d5;
} var_one;

struct s2
{
TYPE2 d1;
TYPE5 d2;
TYPE1 d3;
TYPE3 d4;
TYPE4 d5;
} var_two;

int main(void)
{
printf("s1 offsets:\n");
printf("offseto f(d1) = %d\n", (int)offsetof(s truct s1, d1));
printf("offseto f(d2) = %d\n", (int)offsetof(s truct s1, d2));
printf("offseto f(d3) = %d\n", (int)offsetof(s truct s1, d3));
printf("offseto f(d4) = %d\n", (int)offsetof(s truct s1, d4));
printf("offseto f(d5) = %d\n", (int)offsetof(s truct s1, d5));

printf("\n");
printf("s2 offsets:\n");
printf("offseto f(d1) = %d\n", (int)offsetof(s truct s2, d1));
printf("offseto f(d2) = %d\n", (int)offsetof(s truct s2, d2));
printf("offseto f(d3) = %d\n", (int)offsetof(s truct s2, d3));
printf("offseto f(d4) = %d\n", (int)offsetof(s truct s2, d4));
printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));

return 0;
}
Why do cast the type size_t value yielded by offsetof into int?

<snip>

Mar 7 '07 #7
santosh wrote:
jaysome wrote:
(quelle snippage!)
> printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));

return 0;
}

Why do cast the type size_t value yielded by offsetof into int?
So they can print them using %d?

--
Chris "electric hedgehog" Dollin
The shortcuts are all full of people using them.

Mar 7 '07 #8
Chris Dollin wrote:
santosh wrote:
jaysome wrote:

(quelle snippage!)
printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));

return 0;
}
Why do cast the type size_t value yielded by offsetof into int?

So they can print them using %d?
I know that. I would've myself used %lu, if %zu was not possible,
(since jaysome appears to use Microsoft's Visual C++, which doesn't
implement C99).

So it was a bit strange.

Mar 7 '07 #9
santosh wrote:
Chris Dollin wrote:
>santosh wrote:
>>jaysome wrote:
(quelle snippage!)
>>> printf("offseto f(d5) = %d\n", (int)offsetof(s truct s2, d5));

return 0;
}
Why do cast the type size_t value yielded by offsetof into int?
So they can print them using %d?

I know that. I would've myself used %lu, if %zu was not possible,
So you'd cast to long instead of int.
(since jaysome appears to use Microsoft's Visual C++, which doesn't
implement C99).
Microsoft is one of funny vendors who have long smaller than size_t.

Yevgen
Mar 7 '07 #10

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

Similar topics

1
2973
by: Doug | last post by:
I need to compare two "address" structures within a document, and perform some action if they are not equal. The XML document is a purchase order, with an address at both the header and line item level: <Order> <Header> ... other header level stuff ... <Address>
41
3944
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and Y contains all the elements that are B but not in A. Z will then have the elements that are in both A and B. One way of doing this is of course to iterate throug the lists and compare each of the element, but is there a more efficient way? ...
11
7260
by: Jeff | last post by:
Hi - I'm experiencing a strange problem when comparing 2 guids. In my trial, they're not equal. When I step through the (VB.NET) code, they are evaluated as equal, and when I enter the comparison in the command window, they're not equal. I'm pretty stumped on this one. Please help. I've tried the following structures, all with the same result: guid1.equals(guid2)
19
2649
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor = Color.Empty then..... or if mycolor is Color.Empty then .......
12
25989
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
17
8639
by: junky_fellow | last post by:
Guys, Is it a good way to compare two structures for equality by using "memcmp" ? If not, what could be the problems associated with this ? And what is the correct method of doing this ? thanks for any help.
2
2242
hodgeman
by: hodgeman | last post by:
My second thread on thescripts, so hoping to get the same feedback and help as last time... I've developed an online invoicing and payment system for all my accounts for my web design company. I've been extending the functionality tonight and have come across a problem... I'd like to limit the results based on comparing the total and paid results as below. QUERY SELECT...
3
1658
by: Sean Dalton | last post by:
Hello, I have a two sets OLDLIST and REMOVE. I would like to remove every element in OLDLIST if it is also occuring in REMOVE and store the remaining elements from OLDLIST into NEWLIST. So far, I have read in both lists but I'm struggling comparing the elements
1
7327
by: ladesidude | last post by:
Hi, I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated. #include <stdio.h> #define Size1 (4) #define Size2 (3)
0
8683
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.