Map C++ feature to C

Discussion in 'C Programming' started by zgene, Jun 24, 2012.

  1. zgene

    zgene Guest

    Dear friends

    Is there a way to embulate C++'s virtual destructors in pure C. IE just
    using function pointers.

    Many Thanks.
     
    zgene, Jun 24, 2012
    #1
    1. Advertising

  2. zgene

    Ian Collins Guest

    On 06/24/12 08:23 PM, zgene wrote:
    > Dear friends


    Why is the question completely different form the subject?

    > Is there a way to embulate C++'s virtual destructors in pure C. IE just
    > using function pointers.


    No, because there isn't a way to fully emulate destructors.

    --
    Ian Collins
     
    Ian Collins, Jun 24, 2012
    #2
    1. Advertising

  3. zgene

    BGB Guest

    On 6/24/2012 4:25 AM, Ian Collins wrote:
    > On 06/24/12 08:23 PM, zgene wrote:
    >> Dear friends

    >
    > Why is the question completely different form the subject?
    >
    >> Is there a way to embulate C++'s virtual destructors in pure C. IE just
    >> using function pointers.

    >
    > No, because there isn't a way to fully emulate destructors.
    >


    strictly speaking, yes.

    but if one allows for the case where the destructor is called via the
    object being manually destroyed/"deleted", then it becomes a little
    easier: just have it as a method/function-pointer which is called prior
    to freeing the memory.
     
    BGB, Jun 24, 2012
    #3
  4. zgene

    Paul N Guest

    On Jun 24, 9:23 am, zgene <> wrote:
    > Dear friends
    >
    > Is there a way to embulate C++'s virtual destructors in pure C. IE just
    > using function pointers.
    >
    > Many Thanks.


    How about something like:

    struct Base {
    void *destroy(struct Base *this); // needs to be first member
    /// more here
    };

    struct Derived {
    struct Base b;
    // even more here
    };

    When you create a Base or a Derived, you set the function pointer to
    the correct destructor. Then just before freeing an object, you do:

    (p -> destroy) (p);

    which will work whether p actually points to a Base or a Derived.

    ... completely untested, syntax may be wrong as well...

    Hope that helps.
    Paul.
     
    Paul N, Jun 24, 2012
    #4
  5. zgene

    zgene Guest

    On Sun, 24 Jun 2012 21:25:38 +1200, Ian Collins wrote:

    > On 06/24/12 08:23 PM, zgene wrote:
    >> Dear friends

    >
    > Why is the question completely different form the subject?
    >
    >> Is there a way to embulate C++'s virtual destructors in pure C. IE just
    >> using function pointers.

    >
    > No, because there isn't a way to fully emulate destructors.


    I doubt this answer..... do you have any evidence??
     
    zgene, Jun 24, 2012
    #5
  6. zgene

    Ian Collins Guest

    On 06/25/12 09:33 AM, zgene wrote:
    > On Sun, 24 Jun 2012 21:25:38 +1200, Ian Collins wrote:
    >
    >> On 06/24/12 08:23 PM, zgene wrote:
    >>> Dear friends

    >>
    >> Why is the question completely different form the subject?
    >>
    >>> Is there a way to embulate C++'s virtual destructors in pure C. IE just
    >>> using function pointers.

    >>
    >> No, because there isn't a way to fully emulate destructors.

    >
    > I doubt this answer..... do you have any evidence??


    Show me a way to automatically call a function on leaving a scope in C
    and we can go from there.

    --
    Ian Collins
     
    Ian Collins, Jun 24, 2012
    #6
  7. zgene

    Ian Collins Guest

    On 06/25/12 06:39 AM, BGB wrote:
    > On 6/24/2012 4:25 AM, Ian Collins wrote:
    >> On 06/24/12 08:23 PM, zgene wrote:
    >>> Dear friends

    >>
    >> Why is the question completely different form the subject?
    >>
    >>> Is there a way to embulate C++'s virtual destructors in pure C. IE just
    >>> using function pointers.

    >>
    >> No, because there isn't a way to fully emulate destructors.
    >>

    >
    > strictly speaking, yes.
    >
    > but if one allows for the case where the destructor is called via the
    > object being manually destroyed/"deleted", then it becomes a little
    > easier: just have it as a method/function-pointer which is called prior
    > to freeing the memory.


    A "little" easier is the appropriate term. The OP asked for virtual
    destructor functionality, which requires some form of virtual dispatch
    mechanism. Doable, but ugly.

    --
    Ian Collins
     
    Ian Collins, Jun 24, 2012
    #7
  8. Paul Næ–¼ 2012å¹´6月25日星期一UTC+8上åˆ3時19分00秒寫é“:
    > On Jun 24, 9:23 am, zgene <> wrote:
    > > Dear friends
    > >
    > > Is there a way to embulate C++'s virtual destructors in pure C. IE just
    > > using function pointers.
    > >
    > > Many Thanks.

    >
    > How about something like:
    >
    > struct Base {
    > void *destroy(struct Base *this); // needs to be first member
    > /// more here
    > };
    >
    > struct Derived {
    > struct Base b;
    > // even more here
    > };
    >
    > When you create a Base or a Derived, you set the function pointer to
    > the correct destructor. Then just before freeing an object, you do:
    >
    > (p -> destroy) (p);
    >
    > which will work whether p actually points to a Base or a Derived.
    >
    > ... completely untested, syntax may be wrong as well...
    >
    > Hope that helps.
    > Paul.



    Well, that means it might lead to
    the results of destroying forrests from time
    to time in my understanding
    of the object oriented approach in C++.
     
    88888 Dihedral, Jun 25, 2012
    #8
  9. > Why would you want to do that? If you want to write C++ code, there is
    > an excellent language for that - it is called C++. Much better than
    > some half-arsed attempt to simulate it in C.


    Because C++ may not be allowed (e.g. linux kernel mode) or available (e.g.
    linux IAD) for the target platform.

    Leo Havmøller.
     
    Leo Havmøller, Jun 25, 2012
    #9
  10. zgene

    Jorgen Grahn Guest

    On Mon, 2012-06-25, Leo Havmøller wrote:
    >> Why would you want to do that? If you want to write C++ code, there is
    >> an excellent language for that - it is called C++. Much better than
    >> some half-arsed attempt to simulate it in C.

    >
    > Because C++ may not be allowed (e.g. linux kernel mode) or available (e.g.
    > linux IAD) for the target platform.


    If C++ is disallowed, half-arsed simulations should be, too.
    Especially simulations of tricky features like run-time polymorphism.

    (Note: I'm not arguing against all uses of function pointers. Just
    against unrestricted use of idioms meant to simulate C++ vtables.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jun 25, 2012
    #10
  11. zgene

    BGB Guest

    On 6/24/2012 5:15 PM, Ian Collins wrote:
    > On 06/25/12 06:39 AM, BGB wrote:
    >> On 6/24/2012 4:25 AM, Ian Collins wrote:
    >>> On 06/24/12 08:23 PM, zgene wrote:
    >>>> Dear friends
    >>>
    >>> Why is the question completely different form the subject?
    >>>
    >>>> Is there a way to embulate C++'s virtual destructors in pure C. IE just
    >>>> using function pointers.
    >>>
    >>> No, because there isn't a way to fully emulate destructors.
    >>>

    >>
    >> strictly speaking, yes.
    >>
    >> but if one allows for the case where the destructor is called via the
    >> object being manually destroyed/"deleted", then it becomes a little
    >> easier: just have it as a method/function-pointer which is called prior
    >> to freeing the memory.

    >
    > A "little" easier is the appropriate term. The OP asked for virtual
    > destructor functionality, which requires some form of virtual dispatch
    > mechanism. Doable, but ugly.
    >


    IMO, using C function pointers are not that much drastically worse than
    using C++ virtual methods.

    yes, one either needs to use a vtable, or assign all of the "methods" on
    object construction, but this isn't really a huge issue IMO.


    say, we have something like:
    BaseClass *obj;
    ....
    obj=(BaseClass *)FooClass_New(); //FooClass extends BaseClass
    ....
    BaseClass_Delete(obj); //internally calls FooClass destructor

    with, say:
    void BaseClass_Delete(BaseClass *obj)
    { obj->vt->Destroy(obj); myapp_free(obj); }


    but, hell, it works...
     
    BGB, Jun 25, 2012
    #11
  12. zgene

    BGB Guest

    On 6/25/2012 5:34 AM, Jorgen Grahn wrote:
    > On Mon, 2012-06-25, Leo Havmøller wrote:
    >>> Why would you want to do that? If you want to write C++ code, there is
    >>> an excellent language for that - it is called C++. Much better than
    >>> some half-arsed attempt to simulate it in C.

    >>
    >> Because C++ may not be allowed (e.g. linux kernel mode) or available (e.g.
    >> linux IAD) for the target platform.

    >
    > If C++ is disallowed, half-arsed simulations should be, too.
    > Especially simulations of tricky features like run-time polymorphism.
    >
    > (Note: I'm not arguing against all uses of function pointers. Just
    > against unrestricted use of idioms meant to simulate C++ vtables.)
    >


    it depends on why it is disallowed.

    it may not be due entirely to "policy", but maybe something like lack of
    a good C++ compiler, or the relative difficulty to parse C++ code (vs C)
    via custom-written tools, or interface directly between C++ and a
    scripting language (many have an easier time interfacing with C), ...
     
    BGB, Jun 25, 2012
    #12
  13. zgene

    nroberts Guest

    On Sunday, June 24, 2012 3:12:18 PM UTC-7, Ian Collins wrote:
    > On 06/25/12 09:33 AM, zgene wrote:
    > &gt; On Sun, 24 Jun 2012 21:25:38 +1200, Ian Collins wrote:
    > &gt;
    > &gt;&gt; On 06/24/12 08:23 PM, zgene wrote:
    > &gt;&gt;&gt; Dear friends
    > &gt;&gt;
    > &gt;&gt; Why is the question completely different form the subject?
    > &gt;&gt;
    > &gt;&gt;&gt; Is there a way to embulate C++'s virtual destructors in pure C. IE just
    > &gt;&gt;&gt; using function pointers.
    > &gt;&gt;
    > &gt;&gt; No, because there isn't a way to fully emulate destructors.
    > &gt;
    > &gt; I doubt this answer..... do you have any evidence??
    >
    > Show me a way to automatically call a function on leaving a scope in C
    > and we can go from there.


    This is probably THE most important feature of C++ missing from standard C but it can be done with GCC extensions:

    type var __attribute__(cleanup(destroyType)) = newType();
     
    nroberts, Jul 23, 2012
    #13
  14. On 23/07/2012 18:25, nroberts wrote:
    > On Sunday, June 24, 2012 3:12:18 PM UTC-7, Ian Collins wrote:
    >> On 06/25/12 09:33 AM, zgene wrote:
    >> &gt; On Sun, 24 Jun 2012 21:25:38 +1200, Ian Collins wrote:
    >> &gt;
    >> &gt;&gt; On 06/24/12 08:23 PM, zgene wrote:
    >> &gt;&gt;&gt; Dear friends
    >> &gt;&gt;
    >> &gt;&gt; Why is the question completely different form the subject?
    >> &gt;&gt;
    >> &gt;&gt;&gt; Is there a way to embulate C++'s virtual destructors in pure C. IE just
    >> &gt;&gt;&gt; using function pointers.
    >> &gt;&gt;
    >> &gt;&gt; No, because there isn't a way to fully emulate destructors.
    >> &gt;
    >> &gt; I doubt this answer..... do you have any evidence??
    >>
    >> Show me a way to automatically call a function on leaving a scope in C
    >> and we can go from there.

    >
    > This is probably THE most important feature of C++ missing from standard C but it can be done with GCC extensions:
    >
    > type var __attribute__(cleanup(destroyType)) = newType();
    >


    It is not any feature "missing" from the C standard. C is not C++, nor
    should it be. gcc extensions like this are frankly a bastardisation of
    C, and should be discouraged.

    C, unlike C++ has only one possible interpretation for any valid
    construct. This makes is substantially more predictable, and reduces
    many overheads of the more complex features.

    Take the following C++ code:

    Foo f = Foo(g);

    Care to explain exactly what that statement means? What would be the
    equivalent in meaning for C?

    ~Andrew
     
    Andrew Cooper, Jul 24, 2012
    #14
  15. zgene

    Jorgen Grahn Guest

    On Tue, 2012-07-24, Andrew Cooper wrote:
    > On 23/07/2012 18:25, nroberts wrote:

    ....
    >>
    >> This is probably THE most important feature of C++ missing from standard C
    >> but it can be done with GCC extensions:
    >>
    >> type var __attribute__(cleanup(destroyType)) = newType();
    >>

    >
    > It is not any feature "missing" from the C standard. C is not C++, nor
    > should it be. gcc extensions like this are frankly a bastardisation of
    > C, and should be discouraged.


    I tend to agree.

    > C, unlike C++ has only one possible interpretation for any valid
    > construct. This makes is substantially more predictable, and reduces
    > many overheads of the more complex features.


    Overheads? In compiler complexity, runtime, the reader's head ... ?

    > Take the following C++ code:
    >
    > Foo f = Foo(g);
    >
    > Care to explain exactly what that statement means? What would be the
    > equivalent in meaning for C?


    It means you're left with a Foo object called f, constructed via
    Foo(g), doesn't it? I'm assuming a reasonable implementation of Foo.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jul 24, 2012
    #15
  16. zgene

    aftnix Guest

    On Sunday, June 24, 2012 2:23:02 PM UTC+6, zgene wrote:
    > Dear friends
    >
    > Is there a way to embulate C++'s virtual destructors in pure C. IE just
    > using function pointers.
    >
    > Many Thanks.


    Hi,

    Have you looked into this?

    http://www.planetpdf.com/codecuts/pdfs/ooc.pdf
     
    aftnix, Jul 26, 2012
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    648
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    7,978
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    361
  4. Raymond Hettinger
    Replies:
    30
    Views:
    777
    Steven D'Aprano
    Jan 25, 2006
  5. Raymond Hettinger
    Replies:
    12
    Views:
    1,068
    Raymond Hettinger
    Jan 10, 2006
Loading...

Share This Page