A doubt about struct | | |
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. | | | | re: A doubt about struct
Alex schrieb:[color=blue]
> 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.[/color]
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. | | | | re: A doubt about struct
Alex wrote:[color=blue]
> 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.[/color]
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 | | | | re: A doubt about struct
Michael Mair wrote:[color=blue]
> Alex schrieb:[color=green]
> > 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.[/color]
>
> 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).[/color]
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? | | | | re: A doubt about struct
Alex schrieb:[color=blue]
> Michael Mair wrote:[color=green]
>>Alex schrieb:
>>[color=darkred]
>>>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.[/color]
>>
>>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).[/color]
>
> 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?[/color]
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. | | | | re: A doubt about struct
Michael Mair wrote:[color=blue]
> 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.[/color]
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. | | | | re: A doubt about struct
On Thu, 13 Apr 2006 10:52:16 +0200,
Michael Mair <Michael.Mair@invalid.invalid> wrote
in Msg. <4a6hqfFr0qchU1@individual.net>
[color=blue]
> Yes, this is okay. I am not entirely sure what you want to
> achieve, though.[/color]
Probably some OO-like stuff in C. The GTK+ toolkit used to be written
like that, and now probably GObject is.
robert | | | | re: A doubt about struct
Alex schrieb:[color=blue]
> Michael Mair wrote:
>[color=green]
>>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.[/color]
>
> 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.[/color]
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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|