By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,083 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

A doubt about struct

P: n/a
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know
whether the align in struct will affect such cast.

Apr 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Alex schrieb:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know whether the align in struct will affect such cast.


Do not cast from struct s2 to struct s1.

The standard guarantees to you that the address of the first
member of a struct is the same as the address of the struct
itself.

This means you can cast from "struct s2 *" to a pointer to
the type of the first member, "struct s1 *".

There is no conversion defined between the structure types
themselves. If you need the value, you should reinterpret
the address, i.e. for
struct s1 S1;
struct s2 S2 = {.....};
both are equivalent:
S1 = S2.base;
S1 = *((struct s1 *)&S2);
whereas your compiler should complain about
"S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
former an explicit, the latter an implicit conversion to
struct s1).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 13 '06 #2

P: n/a
Alex wrote:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know
whether the align in struct will affect such cast.


You can only cast scalar types, not composite types like structures, so
I assume that you mean a cast from (struct s2 *) to (struct s1 *). The
C99 standard (6.7.2.1) specifies that a pointer to a structure object,
suitably converted, points to its initial member. Therefore, your
proposed cast is legal and guaranteed to work.

--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality
Apr 13 '06 #3

P: n/a

Michael Mair wrote:
Alex schrieb:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know whether the align in struct will affect such cast.


Do not cast from struct s2 to struct s1.

The standard guarantees to you that the address of the first
member of a struct is the same as the address of the struct
itself.

This means you can cast from "struct s2 *" to a pointer to
the type of the first member, "struct s1 *".

There is no conversion defined between the structure types
themselves. If you need the value, you should reinterpret
the address, i.e. for
struct s1 S1;
struct s2 S2 = {.....};
both are equivalent:
S1 = S2.base;
S1 = *((struct s1 *)&S2);
whereas your compiler should complain about
"S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
former an explicit, the latter an implicit conversion to
struct s1).


I cannot express my idea well, my intention is pointer cast.
So, if I do like this:
struct s1* p = malloc(sizeof(struct s2));
And then, I use(read or write) some members of struct s1 via the
pointer p, is it OK?

Apr 13 '06 #4

P: n/a
Alex schrieb:
Michael Mair wrote:
Alex schrieb:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know whether the align in struct will affect such cast.


Do not cast from struct s2 to struct s1.

The standard guarantees to you that the address of the first
member of a struct is the same as the address of the struct
itself.

This means you can cast from "struct s2 *" to a pointer to
the type of the first member, "struct s1 *".

There is no conversion defined between the structure types
themselves. If you need the value, you should reinterpret
the address, i.e. for
struct s1 S1;
struct s2 S2 = {.....};
both are equivalent:
S1 = S2.base;
S1 = *((struct s1 *)&S2);
whereas your compiler should complain about
"S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
former an explicit, the latter an implicit conversion to
struct s1).


I cannot express my idea well, my intention is pointer cast.
So, if I do like this:
struct s1* p = malloc(sizeof(struct s2));
And then, I use(read or write) some members of struct s1 via the
pointer p, is it OK?


Yes, this is okay. I am not entirely sure what you want to
achieve, though. The above is a little bit too short.
Please give us a little bit more context, e.g. whether you
have a function allocating new storage for struct s2 and you
want to use this just as struct s1 or even a little bit more.
Maybe there is an even better way of doing what you want to
do.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 13 '06 #5

P: n/a

Michael Mair wrote:
Yes, this is okay. I am not entirely sure what you want to
achieve, though. The above is a little bit too short.
Please give us a little bit more context, e.g. whether you
have a function allocating new storage for struct s2 and you
want to use this just as struct s1 or even a little bit more.
Maybe there is an even better way of doing what you want to
do.


What I want to do is something like Interface in Java or abstract class
in C++.
In the struct s1, I will declare some function pointers, which I will
not
assign for them. And in struct s2, I will delcare a object of struct
s1,
then assign for the function pointers of the object. The intention of
doing
all of above is to implement simple OOP using C.( Because In my
enviroment,
I cannot use other language.)
So, if you can provide any information , even a link, about how to
implement simple
OOP, I will appreciate your help very much.

Apr 13 '06 #6

P: n/a
On Thu, 13 Apr 2006 10:52:16 +0200,
Michael Mair <Mi**********@invalid.invalid> wrote
in Msg. <4a************@individual.net>
Yes, this is okay. I am not entirely sure what you want to
achieve, though.


Probably some OO-like stuff in C. The GTK+ toolkit used to be written
like that, and now probably GObject is.

robert
Apr 13 '06 #7

P: n/a
Alex schrieb:
Michael Mair wrote:
Yes, this is okay. I am not entirely sure what you want to
achieve, though. The above is a little bit too short.
Please give us a little bit more context, e.g. whether you
have a function allocating new storage for struct s2 and you
want to use this just as struct s1 or even a little bit more.
Maybe there is an even better way of doing what you want to
do.


What I want to do is something like Interface in Java or
abstract class in C++.
In the struct s1, I will declare some function pointers, which
I will not assign for them. And in struct s2, I will delcare a
object of struct s1, then assign for the function pointers of
the object. The intention of doing all of above is to implement
simple OOP using C.( Because In my enviroment, I cannot use
other language.)
So, if you can provide any information , even a link, about how
to implement simple OOP, I will appreciate your help very much.


There have been several discussions about this in comp.lang.c;
maybe you can find something useful searching the archives:

<http://groups.google.de/groups?as_q=OOP+in+C&num=30&scoring=r&hl=de&as_epq =&as_oq=&as_eq=&as_ugroup=comp.lang.c&as_usubject= &as_uauthors=&lr=&as_qdr=&as_drrb=b&as_mind=1&as_m inm=1&as_miny=2004&as_maxd=17&as_maxm=4&as_maxy=20 06>

The basic technique of "typesafe casting", always remains the
same: You have a pointer to a structure; you know that the
first member of this structure always is or contains (maybe
over several levels) a structure describing a "parent class".
You always can cast a pointer of a "child class" to a pointer
of the "parent class". This of course restricts you to single
inheritance.
Often, all "classes" are derived from one generic "class" which
may even offer a way to find out the "type" of the "class" it
is a member of. In this case, it is safe to cast "from small to
large", i.e. from the pointer of the "parent class" to a pointer
to the "child class"; this can become the source of merry
confusion if not done right.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.