This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Is strongly-typed programming conceivable for C code using gcc ?
- From: "Dima Sorkin" <dima dot sorkin at gmail dot com>
- To: "John Love-Jensen" <eljay at adobe dot com>, "Alexandre Beaugy" <beaugy dot ext at tlse dot sofreavia dot fr>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 19 Sep 2007 17:24:44 +0200
- Subject: Re: Is strongly-typed programming conceivable for C code using gcc ?
- References: <016101c7fac6$cec89930$4121820a@tlse.sofreavia.fr> <C316A5DA.252FD%eljay@adobe.com>
Hi, Alexandre.
It seems that you are starting to write some project using the ADT technique,
on C. Here some link to a book I didn't read. (I never programmed on C).
Our lecturers say that this book is not _so_ good, but it is what
de facto they want to teach on courses on C, so it is recommended.
http://www.cs.technion.ac.il/users/yechiel/CS/Interesting/BugFreeC.html
Regards,
Dima.
On 9/19/07, John Love-Jensen <eljay@adobe.com> wrote:
> Hi Alexandre,
>
> Taking Dima's solution in conjunction with the way Apple solved their
> similar problem by using opaque types in Cocoa and Carbon:
>
> struct CW_C_MsgUp_s;
> typedef struct CW_C_MsgUp_t* CW_C_MsgUp_t;
>
> struct CW_C_MsgDown_s;
> typedef struct CW_C_MsgDown_t* CW_C_MsgDown_t;
>
> This will insulate your implementation of those opaque types from the public
> header files (note that the structs are forward references, and not declared
> publically), and allow you some measure of type safety in C.
>
> Apple uses a factory function to create their opaque types, and for most
> opaque types the CFRelease function to destruct them (using a reference
> counting mechanism). That's the role that CW_C_MsgUp_new() and
> CW_C_MsgDown_new() provides.
>
> As far as them being pointers... I believe that you will want them to be
> pointers baked into the typedef.
>
> The one change to your code would be to change from this...
>
> CW_C_MsgUp_t *my_up_msg;
> CW_C_MsgDown_t *my_down_msg;
>
> ...to this...
>
> CW_C_MsgUp_t my_up_msg;
> CW_C_MsgDown_t my_down_msg;
>
> HTH,
> --Eljay
>
>