A doubt about struct

A

Alex

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.
 
M

Michael Mair

Alex said:
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
 
D

Diomidis Spinellis

Alex said:
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.
 
A

Alex

Michael said:
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?
 
M

Michael Mair

Alex said:
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
 
A

Alex

Michael said:
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.
 
R

Robert Latest

On Thu, 13 Apr 2006 10:52:16 +0200,
in Msg. said:
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
 
M

Michael Mair

Alex said:
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...s_miny=2004&as_maxd=17&as_maxm=4&as_maxy=2006>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top