473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

structures compatibility

Hi,

If I define two structures like this

struct s1 {

int a;
int b;
}

struct s2 {
int a;
int b;
int c;
}

and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
structure.

Can I use ps2 as a pointer on an s1 sturcture and have the a and b
correct? In ther words, does the order of the different data in a
structure follow the order in the declaration?

Thanks.

--
Saïd.

Nov 13 '05 #1
8 1746
Saïd <sa****@spamquatramaran.ens.fr> scribbled the following:
Hi, If I define two structures like this struct s1 { int a;
int b;
} struct s2 {
int a;
int b;
int c;
} and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
structure. Can I use ps2 as a pointer on an s1 sturcture and have the a and b
correct? In ther words, does the order of the different data in a
structure follow the order in the declaration?


The order does, but the offsets don't have to. IOW, you are guaranteed
that the fields in struct s1 begin in the order a, b and the fields in
struct s2 begin in the order a, b, c. You are furthermore guaranteed
that the field a in struct s1 begins at the same offset as the field a
in struct s2.
You aren't guaranteed anything else. Most importantly, you aren't
guaranteed that the field b in struct s1 begins at the same offset as
the field b in struct s2. There might be a difference of thousands of
bytes, given a particularly nasty implementation.
Therefore the answer to whether you can use pointers to struct s2 as
pointers to struct s1 is "it depends on your implementation".
If struct s1 only consisted of the field a, the answer would be "yes,
knock yourself out". But as it has more fields the answer is not so
simple.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
Nov 13 '05 #2
Joona I Palaste wrote:
Saïd <sa****@spamquatramaran.ens.fr> scribbled the following:
Hi,


If I define two structures like this


struct s1 {


int a;
int b;
}


struct s2 {
int a;
int b;
int c;
}


and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
structure.


Can I use ps2 as a pointer on an s1 sturcture and have the a and b
correct? In ther words, does the order of the different data in a
structure follow the order in the declaration?

The order does, but the offsets don't have to. IOW, you are guaranteed
that the fields in struct s1 begin in the order a, b and the fields in
struct s2 begin in the order a, b, c. You are furthermore guaranteed
that the field a in struct s1 begins at the same offset as the field a
in struct s2.
You aren't guaranteed anything else. Most importantly, you aren't
guaranteed that the field b in struct s1 begins at the same offset as
the field b in struct s2. There might be a difference of thousands of
bytes, given a particularly nasty implementation.


And what about a specific implementastion as gcc for linux?

--
Saïd.

Nov 13 '05 #3
Saïd <sa****@spamquatramaran.ens.fr> scribbled the following:
And what about a specific implementastion as gcc for linux?


They are off-topic for comp.lang.c. For gcc, there's gnu.gcc.help to ask
in.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush
Nov 13 '05 #4
Joona I Palaste wrote:

Saïd <sa****@spamquatramaran.ens.fr> scribbled the following:
Hi,

If I define two structures like this

struct s1 {

int a;
int b;
}

struct s2 {
int a;
int b;
int c;
}

and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
structure.

Can I use ps2 as a pointer on an s1 sturcture and have the a and b
correct? In ther words, does the order of the different data in a
structure follow the order in the declaration?


The order does, but the offsets don't have to. IOW, you are guaranteed
that the fields in struct s1 begin in the order a, b and the fields in
struct s2 begin in the order a, b, c. You are furthermore guaranteed
that the field a in struct s1 begins at the same offset as the field a
in struct s2.
You aren't guaranteed anything else. Most importantly, you aren't
guaranteed that the field b in struct s1 begins at the same offset as
the field b in struct s2. There might be a difference of thousands of
bytes, given a particularly nasty implementation.
Therefore the answer to whether you can use pointers to struct s2 as
pointers to struct s1 is "it depends on your implementation".
If struct s1 only consisted of the field a, the answer would be "yes,
knock yourself out". But as it has more fields the answer is not so
simple.


Joona is right (I think), but as a practical matter you'll
find that offsetof(struct s1, b) == offsetof(struct s2, b).

The guarantee of equal offsets *does* hold if there's a
union with the two structs as elements:

union u { struct s1 s1; struct s2 s2; };

... because there's a special rule in the Standard to cover
this case. Now, the practical difficulty: How can a compiler
determine that no such union exists? Even if no such union
exists in the current translation unit, there might be such
a union declared in another translation unit somewhere else,
perhaps in source code that hasn't even been written yet but
will be written and compiled tomorrow and then linked with
this one. So a compiler would need to be almost supernaturally
clever to determine whetherr it was safe to let the offsets
disagree -- and the people who write compilers have more
important things to spend their time on than deviousness and
perversity (it doesn't always seem that way, but ...)

Summary: Technically and legalistically, you have no
guarantee about the offsets. In practice, though, you do.

--
Er*********@sun.com
Nov 13 '05 #5
I think a more reliable way to achieve the OP's
intent is to imbed the first struct within the
second struct.

struct s1 {
int a;
int b;
};

struct s2 {
struct s1 s1;
int c;
};

Then passing a s2 pointer as a s1 pointer should
yield the desired behavior.
--
----------------------------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
303-774-9381
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
"Eric Sosman" <Er*********@sun.com> wrote in message
news:3F***************@sun.com...
Joona I Palaste wrote:

Saïd <sa****@spamquatramaran.ens.fr> scribbled the following:
Hi,

If I define two structures like this

struct s1 {

int a;
int b;
}

struct s2 {
int a;
int b;
int c;
}

and if I have ps2 a pointer on an s2 structure, ps1 a pointer on an s1
structure.

Can I use ps2 as a pointer on an s1 sturcture and have the a and b
correct? In ther words, does the order of the different data in a
structure follow the order in the declaration?


The order does, but the offsets don't have to. IOW, you are guaranteed
that the fields in struct s1 begin in the order a, b and the fields in
struct s2 begin in the order a, b, c. You are furthermore guaranteed
that the field a in struct s1 begins at the same offset as the field a
in struct s2.
You aren't guaranteed anything else. Most importantly, you aren't
guaranteed that the field b in struct s1 begins at the same offset as
the field b in struct s2. There might be a difference of thousands of
bytes, given a particularly nasty implementation.
Therefore the answer to whether you can use pointers to struct s2 as
pointers to struct s1 is "it depends on your implementation".
If struct s1 only consisted of the field a, the answer would be "yes,
knock yourself out". But as it has more fields the answer is not so
simple.


Joona is right (I think), but as a practical matter you'll
find that offsetof(struct s1, b) == offsetof(struct s2, b).

The guarantee of equal offsets *does* hold if there's a
union with the two structs as elements:

union u { struct s1 s1; struct s2 s2; };

.... because there's a special rule in the Standard to cover
this case. Now, the practical difficulty: How can a compiler
determine that no such union exists? Even if no such union
exists in the current translation unit, there might be such
a union declared in another translation unit somewhere else,
perhaps in source code that hasn't even been written yet but
will be written and compiled tomorrow and then linked with
this one. So a compiler would need to be almost supernaturally
clever to determine whetherr it was safe to let the offsets
disagree -- and the people who write compilers have more
important things to spend their time on than deviousness and
perversity (it doesn't always seem that way, but ...)

Summary: Technically and legalistically, you have no
guarantee about the offsets. In practice, though, you do.

--
Er*********@sun.com
Nov 13 '05 #6
Okay why does the OP's structs not have a semi-colon after the ending brace
and the struct posting of xarax does have semi-colons?

Bill

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 13 '05 #7
Bill Cunningham <nospam@net> scribbled the following:
Okay why does the OP's structs not have a semi-colon after the ending brace
and the struct posting of xarax does have semi-colons?


They should both have ending semicolons. The OP made a typo.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
Nov 13 '05 #8
Joona I Palaste wrote:
Bill Cunningham <nospam@net> scribbled the following:
Okay why does the OP's structs not have a semi-colon after the ending brace
and the struct posting of xarax does have semi-colons?

They should both have ending semicolons. The OP made a typo.


OP means me? Yes I was just presenting the structures, not actually
coding them. :)

I'll add a union to make sure the two structures have the same beginig
even if i don't use this union in my programs.

Thanks to every body.

--
Saïd.

Nov 13 '05 #9

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

Similar topics

6
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run...
18
by: Markus.Elfring | last post by:
The C++ language specification provides the key word "mutable" that is not available in the C99 standard. Will it be imported to reduce any incompatibilities?...
14
by: frostalicious | last post by:
Used VB.NET (on my client PC) to convert VB6 executable to .NET executable. Placed the .exe file on a network drive on my server. From client, ran .NET Wizards "Trust an Assembly" to make the...
15
by: roberts.noah | last post by:
I ran across some code that called memset(this, 0, sizeof(*this)) in the member function of a structure in C++. This just looked wrong to me so I did some web searching and it does indeed seem...
5
by: Sandeep | last post by:
struct x { int a; char b; int c; char d; }; typedef struct x X;
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
2
by: Carlo | last post by:
I recently started in a new position, and I inherited an application written in VB6 that uses a bunch of DLLs and OCX controls. Version Compatibility is set to Binary at the project level, but since...
29
by: Mik0b0 | last post by:
Hallo to everyone. This fall I am going to start data structures as a part of C language course. The problem is I could not find any satisfying tutorial about structures in C. There are plenty of...
32
by: Paulo J. Matos | last post by:
Hi all, I am a Python beginner, reading through 2.6 tutorial. I am wondering where are structures? On the other hand, I think I might have the answer. Since Python focus on having one way to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.