Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #22602 > unrolled thread
| Started by | zgene <nospam@nospam.com> |
|---|---|
| First post | 2012-06-24 08:23 +0000 |
| Last post | 2012-07-26 13:36 -0700 |
| Articles | 18 — 12 participants |
Back to article view | Back to comp.lang.c
Map C++ feature to C zgene <nospam@nospam.com> - 2012-06-24 08:23 +0000
Re: Map C++ feature to C Ian Collins <ian-news@hotmail.com> - 2012-06-24 21:25 +1200
Re: Map C++ feature to C BGB <cr88192@hotmail.com> - 2012-06-24 13:39 -0500
Re: Map C++ feature to C Ian Collins <ian-news@hotmail.com> - 2012-06-25 10:15 +1200
Re: Map C++ feature to C BGB <cr88192@hotmail.com> - 2012-06-25 11:32 -0500
Re: Map C++ feature to C zgene <nospam@nospam.com> - 2012-06-24 21:33 +0000
Re: Map C++ feature to C Ian Collins <ian-news@hotmail.com> - 2012-06-25 10:12 +1200
Re: Map C++ feature to C William Ahern <william@wilbur.25thandClement.com> - 2012-06-24 18:49 -0700
Re: Map C++ feature to C nroberts <roberts.noah@gmail.com> - 2012-07-23 10:25 -0700
Re: Map C++ feature to C Andrew Cooper <amc96@cam.ac.uk> - 2012-07-24 01:34 +0100
Re: Map C++ feature to C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-07-24 15:58 +0000
Re: Map C++ feature to C "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-06-24 11:36 -0700
Re: Map C++ feature to C Leo Havmøller <rtxleh@nospam.nospam> - 2012-06-25 05:43 +0200
Re: Map C++ feature to C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-06-25 10:34 +0000
Re: Map C++ feature to C BGB <cr88192@hotmail.com> - 2012-06-25 11:36 -0500
Re: Map C++ feature to C Paul N <gw7rib@aol.com> - 2012-06-24 12:19 -0700
Re: Map C++ feature to C 88888 Dihedral <dihedral88888@googlemail.com> - 2012-06-24 19:49 -0700
Re: Map C++ feature to C aftnix <aftnix@gmail.com> - 2012-07-26 13:36 -0700
| From | zgene <nospam@nospam.com> |
|---|---|
| Date | 2012-06-24 08:23 +0000 |
| Subject | Map C++ feature to C |
| Message-ID | <js6ip6$qb1$1@speranza.aioe.org> |
Dear friends Is there a way to embulate C++'s virtual destructors in pure C. IE just using function pointers. Many Thanks.
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2012-06-24 21:25 +1200 |
| Message-ID | <a4o4ojFe9mU2@mid.individual.net> |
| In reply to | #22602 |
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
[toc] | [prev] | [next] | [standalone]
| From | BGB <cr88192@hotmail.com> |
|---|---|
| Date | 2012-06-24 13:39 -0500 |
| Message-ID | <js7n1a$buh$1@news.albasani.net> |
| In reply to | #22605 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2012-06-25 10:15 +1200 |
| Message-ID | <a4phrcFaukU2@mid.individual.net> |
| In reply to | #22616 |
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
[toc] | [prev] | [next] | [standalone]
| From | BGB <cr88192@hotmail.com> |
|---|---|
| Date | 2012-06-25 11:32 -0500 |
| Message-ID | <jsa3v8$vjl$1@news.albasani.net> |
| In reply to | #22630 |
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...
[toc] | [prev] | [next] | [standalone]
| From | zgene <nospam@nospam.com> |
|---|---|
| Date | 2012-06-24 21:33 +0000 |
| Message-ID | <js812m$ak3$1@speranza.aioe.org> |
| In reply to | #22605 |
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??
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2012-06-25 10:12 +1200 |
| Message-ID | <a4phm2FaukU1@mid.individual.net> |
| In reply to | #22625 |
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
[toc] | [prev] | [next] | [standalone]
| From | William Ahern <william@wilbur.25thandClement.com> |
|---|---|
| Date | 2012-06-24 18:49 -0700 |
| Message-ID | <imskb9-2u7.ln1@wilbur.25thandClement.com> |
| In reply to | #22628 |
Ian Collins <ian-news@hotmail.com> wrote: > 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. You could do it the way "portable" alloca implementations work, though destruction would be lazy, like GC'd langauages.
[toc] | [prev] | [next] | [standalone]
| From | nroberts <roberts.noah@gmail.com> |
|---|---|
| Date | 2012-07-23 10:25 -0700 |
| Message-ID | <caf65604-f87f-4e89-a59e-56db88999717@googlegroups.com> |
| In reply to | #22628 |
On Sunday, June 24, 2012 3:12:18 PM UTC-7, Ian Collins wrote: > 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. 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();
[toc] | [prev] | [next] | [standalone]
| From | Andrew Cooper <amc96@cam.ac.uk> |
|---|---|
| Date | 2012-07-24 01:34 +0100 |
| Message-ID | <24mPr.29428$ja1.16975@fx33.am4> |
| In reply to | #23621 |
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: >> > 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. > > 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
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2012-07-24 15:58 +0000 |
| Message-ID | <slrnk0thgm.ph6.grahn+nntp@frailea.sa.invalid> |
| In reply to | #23640 |
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 .
[toc] | [prev] | [next] | [standalone]
| From | "christian.bau" <christian.bau@cbau.wanadoo.co.uk> |
|---|---|
| Date | 2012-06-24 11:36 -0700 |
| Message-ID | <54650c61-f6ea-47ce-9c72-c9083832b2e1@n5g2000vbb.googlegroups.com> |
| In reply to | #22602 |
On Jun 24, 9:23 am, zgene <nos...@nospam.com> wrote: > Dear friends > > Is there a way to embulate C++'s virtual destructors in pure C. IE just > using function pointers. > > Many Thanks. 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.
[toc] | [prev] | [next] | [standalone]
| From | Leo Havmøller <rtxleh@nospam.nospam> |
|---|---|
| Date | 2012-06-25 05:43 +0200 |
| Message-ID | <js8mp2$khc$1@speranza.aioe.org> |
| In reply to | #22617 |
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2012-06-25 10:34 +0000 |
| Message-ID | <slrnjugfl7.rk7.grahn+nntp@frailea.sa.invalid> |
| In reply to | #22638 |
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 .
[toc] | [prev] | [next] | [standalone]
| From | BGB <cr88192@hotmail.com> |
|---|---|
| Date | 2012-06-25 11:36 -0500 |
| Message-ID | <jsa46r$dn$1@news.albasani.net> |
| In reply to | #22666 |
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), ...
[toc] | [prev] | [next] | [standalone]
| From | Paul N <gw7rib@aol.com> |
|---|---|
| Date | 2012-06-24 12:19 -0700 |
| Message-ID | <9d253d89-ec75-4e71-a844-5404de7159ac@z19g2000vbe.googlegroups.com> |
| In reply to | #22602 |
On Jun 24, 9:23 am, zgene <nos...@nospam.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-06-24 19:49 -0700 |
| Message-ID | <d916a347-5105-4812-84fb-8a5ddae457e0@googlegroups.com> |
| In reply to | #22619 |
Paul N於 2012年6月25日星期一UTC+8上午3時19分00秒寫道:
> On Jun 24, 9:23 am, zgene <nos...@nospam.com> 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++.
[toc] | [prev] | [next] | [standalone]
| From | aftnix <aftnix@gmail.com> |
|---|---|
| Date | 2012-07-26 13:36 -0700 |
| Message-ID | <7c3115d5-6098-4365-8ebe-8e64da19f2cb@googlegroups.com> |
| In reply to | #22602 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web