f



Exceptions handling in pure C

How can translate C++ to C code with exceptions handling? Unfortunately 
Stroustrup translator cfron has only source and not compiled, llvm command 
to translate C++ => C not works. 

0
borneq1 (31)
12/7/2012 6:50:09 AM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

28 Replies
627 Views

Similar Articles

[PageSpeed] 39

Borneq wrote:
> How can translate C++ to C code with exceptions handling? Unfortunately
> Stroustrup translator cfron has only source and not compiled, llvm command
> to translate C++ => C not works.

Two options:

Start from scratch.
Use Comeau http://www.comeaucomputing.com/

-- 
Ian Collins
0
ian-news (10155)
12/7/2012 7:42:08 AM
Użytkownik "Ian Collins" <ian-news@hotmail.com> napisał w wiadomości 
news:aidkuhFac77U1@mid.individual.net...
> Start from scratch.

How start? I haven't information about exception handling in C

> Use Comeau http://www.comeaucomputing.com/

Online http://www.comeaucomputing.com/tryitout/ can translate to C or only 
to assembler? 

0
borneq1 (31)
12/7/2012 8:23:10 AM
Borneq wrote:
> Użytkownik "Ian Collins" <ian-news@hotmail.com> napisał w wiadomości
> news:aidkuhFac77U1@mid.individual.net...
>> Start from scratch.
>
> How start? I haven't information about exception handling in C

There isn't any, you have to rewrite the code from scratch.

>> Use Comeau http://www.comeaucomputing.com/
>
> Online http://www.comeaucomputing.com/tryitout/ can translate to C or only
> to assembler?

Neither.  You have to buy the compiler.

-- 
Ian Collins
0
ian-news (10155)
12/7/2012 8:41:14 AM
On 12/7/2012 2:41 AM, Ian Collins wrote:
> Borneq wrote:
>> Użytkownik "Ian Collins" <ian-news@hotmail.com> napisał w wiadomości
>> news:aidkuhFac77U1@mid.individual.net...
>>> Start from scratch.
>>
>> How start? I haven't information about exception handling in C
>
> There isn't any, you have to rewrite the code from scratch.
>

well, it is possible, either to use OS-specific mechanisms (like Win32 
SEH), or write ones' own exception-handling system (potentially 
involving linked-lists and longjmp), and rewrite any code to use this.

it is kind-of ugly, but it sort of works...


>>> Use Comeau http://www.comeaucomputing.com/
>>
>> Online http://www.comeaucomputing.com/tryitout/ can translate to C or
>> only
>> to assembler?
>
> Neither.  You have to buy the compiler.
>

0
cr88192355 (1928)
12/7/2012 9:10:10 AM
On 12/07/2012 01:50 AM, Borneq wrote:
> How can translate C++ to C code with exceptions handling? Unfortunately 
> Stroustrup translator cfron has only source and not compiled, llvm command 
> to translate C++ => C not works. 

C doesn't have exception handling; it's one of the many features that
C++ added to C. You can use setjmp()/longjmp() to implement a similar
functionality, but doing so is much more complicated than having
exceptions as a built-in feature of the language. That's precisely why
Stroustrup put exceptions in C++.

One of the more important complications has to do with destructors,
another C++ feature that doesn't exist in C. In C++, objects with
automatic storage duration and class type have their destructors
automatically called when their lifetime ends, even if it ends as a
result of an exception being thrown. Furthermore, it is guaranteed that
the destructors will be called in the reverse of the order in which the
constructors were called. When you translate your code to C, you'll have
to move the clean-up code from your C++ destructors into ordinary C
functions, and you'll have to insert explicit calls to them, rather than
relying on them being called implicitly. In particular, you need to make
sure that they get called even when you call longjmp() to emulate C++
exception handling. This is going to require that you keep track, in
some manner, of the list of destructors that need to be called; given
that the corresponding setjmp() will often be in a very different
section of the code, keeping track of which destructors need to be
called is non-trivial.

Redesigning your code so it doesn't rely upon exception handling is
painful, but can result in better C code than trying to translate C++
exception handling directly.

On the other hand: if your entire program never actually handles
exceptions, then you may be able to adequately emulate exceptions simply
by replacing throw() with exit(). That's the case for the C++ code I'm
working on right now.
-- 
James Kuyper
0
jameskuyper (5635)
12/7/2012 1:06:06 PM
Użytkownik "James Kuyper" <jameskuyper@verizon.net> napisał w wiadomości 
news:k9spjv$8cb$1@dont-email.me...
> On the other hand: if your entire program never actually handles
> exceptions, then you may be able to adequately emulate exceptions simply
> by replacing throw() with exit(). That's the case for the C++ code I'm
> working on right now.

Is http://www.microsoft.com/msj/0197/Exception/Exception.aspx with code:
int main()
{
    DWORD handler = (DWORD)_except_handler;

    __asm
    {                           // Build EXCEPTION_REGISTRATION record:
        push    handler         // Address of handler function
        push    FS:[0]          // Address of previous handler
        mov     FS:[0],ESP      // Install new EXECEPTION_REGISTRATION
    }

    __asm
    {
        mov     eax,0           // Zero out EAX
        mov     [eax], 1        // Write to EAX to deliberately cause a 
fault
    }

    printf( "After writing!\n" );

    __asm
    {                           // Remove our EXECEPTION_REGISTRATION record
        mov     eax,[ESP]       // Get pointer to previous record
        mov     FS:[0], EAX     // Install previous record
        add     esp, 8          // Clean our EXECEPTION_REGISTRATION off 
stack
    }

    return 0;
}


0
borneq1 (31)
12/7/2012 3:40:28 PM
On 12/07/2012 10:40 AM, Borneq wrote:
> Użytkownik "James Kuyper" <jameskuyper@verizon.net> napisał w wiadomości 
> news:k9spjv$8cb$1@dont-email.me...
>> On the other hand: if your entire program never actually handles
>> exceptions, then you may be able to adequately emulate exceptions simply
>> by replacing throw() with exit(). That's the case for the C++ code I'm
>> working on right now.
> 
> Is http://www.microsoft.com/msj/0197/Exception/Exception.aspx with code:
> int main()
> {
>     DWORD handler = (DWORD)_except_handler;
> 
>     __asm
>     {                           // Build EXCEPTION_REGISTRATION record:
>         push    handler         // Address of handler function
>         push    FS:[0]          // Address of previous handler
>         mov     FS:[0],ESP      // Install new EXECEPTION_REGISTRATION
>     }
> 
>     __asm
>     {
>         mov     eax,0           // Zero out EAX
>         mov     [eax], 1        // Write to EAX to deliberately cause a 
> fault
>     }
> 
>     printf( "After writing!\n" );
> 
>     __asm
>     {                           // Remove our EXECEPTION_REGISTRATION record
>         mov     eax,[ESP]       // Get pointer to previous record
>         mov     FS:[0], EAX     // Install previous record
>         add     esp, 8          // Clean our EXECEPTION_REGISTRATION off 
> stack
>     }
> 
>     return 0;
> }

Your title said "pure C". The __asm keyword is not pure C; nor is
anything that's microsoft-specific. There's nothing wrong with writing
microsoft-specific code, if you're willing to tie your code to
microsoft, but if you're willing to consider solutions of that type, you
should have changed the Subject: line accordingly, and you should have
posted your message to a forum specializing in programming for Win32.

0
jameskuyper (5635)
12/7/2012 4:24:09 PM
Użytkownik "James Kuyper" <jameskuyper@verizon.net> napisał w wiadomości 
news:50C21829.9090705@verizon.net...
> On 12/07/2012 10:40 AM, Borneq wrote:
> Your title said "pure C". The __asm keyword is not pure C; nor is
> anything that's microsoft-specific. There's nothing wrong with writing
> microsoft-specific code, if you're willing to tie your code to
> microsoft, but if you're willing to consider solutions of that type, you
> should have changed the Subject: line accordingly, and you should have
> posted your message to a forum specializing in programming for Win32.

Not exactly the I expressed what I have in mind. "Pure" - I think no C++ but 
C, also assebler may be. It is interesting not only for Win32 (or Win64) but 
also for Linux. 

0
borneq1 (31)
12/7/2012 5:02:39 PM
On 12/07/2012 12:02 PM, Borneq wrote:
> Użytkownik "James Kuyper" <jameskuyper@verizon.net> napisał w wiadomości 
> news:50C21829.9090705@verizon.net...
>> On 12/07/2012 10:40 AM, Borneq wrote:
>> Your title said "pure C". The __asm keyword is not pure C; nor is
>> anything that's microsoft-specific. There's nothing wrong with writing
>> microsoft-specific code, if you're willing to tie your code to
>> microsoft, but if you're willing to consider solutions of that type, you
>> should have changed the Subject: line accordingly, and you should have
>> posted your message to a forum specializing in programming for Win32.
> 
> Not exactly the I expressed what I have in mind. "Pure" - I think no C++ but 
> C, also assebler may be. It is interesting not only for Win32 (or Win64) but 
> also for Linux. 

Well, if it includes a assembler, you should not describe it as "pure
C". It would be more accurate to describe it as "C plus assembler".
If you're interested in a single solution that works for Linux as well
as Win32, the one you found at that Microsoft web site isn't that solution.
0
jameskuyper (5635)
12/7/2012 5:25:51 PM
There are situations where it doesn't make sense to say "how do I do
this C++ thing in C"; the proper answer would be "just do it in C++".
Exception handling is one of those things.

When an exception is thrown, you have to clean up resources as the
exception winds its way through the call chain. In Java or C++ there
are things in the language that do this clean up automatically.
Without that, it is impossible for a mere human to always do that
cleanup correctly. Forget it.
0
12/7/2012 5:29:20 PM
BGB wrote:
> On 12/7/2012 2:41 AM, Ian Collins wrote:
>> Borneq wrote:
>>> Użytkownik "Ian Collins" <ian-news@hotmail.com> napisał w wiadomości
>>> news:aidkuhFac77U1@mid.individual.net...
>>>> Start from scratch.
>>>
>>> How start? I haven't information about exception handling in C
>>
>> There isn't any, you have to rewrite the code from scratch.
>>
>
> well, it is possible, either to use OS-specific mechanisms (like Win32
> SEH), or write ones' own exception-handling system (potentially
> involving linked-lists and longjmp), and rewrite any code to use this.
>
> it is kind-of ugly, but it sort of works...

It would also be fraught with danger.  Writing exception safe code in C 
isn't easy.

-- 
Ian Collins
0
ian-news (10155)
12/7/2012 6:44:23 PM
"Borneq" <borneq@antyspam.hidden.pl> writes:
> How can translate C++ to C code with exceptions handling? Unfortunately 
> Stroustrup translator cfron has only source and not compiled, llvm command 
> to translate C++ => C not works. 

We can probably give you a better answer if you let us know *why*
you want to translate the C++ code to C.  How does leaving the
code in C++ and compiling it with a C++ compiler not satisfy your
requirements?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
kst-u (21963)
12/7/2012 7:07:21 PM
Uzytkownik "Keith Thompson" <kst-u@mib.org> napisal w wiadomosci 
news:ln38zhptp2.fsf@nuthaus.mib.org...
> We can probably give you a better answer if you let us know *why*
> you want to translate the C++ code to C.  How does leaving the
> code in C++ and compiling it with a C++ compiler not satisfy your
> requirements?

I wanna for training reasons write compiler (rightly translator) from my own 
object language to C+asm. It will be well if it support exceptions and 
multiple Inheritance. No garbage collector -  this language will be 
traditional.
I try install llvm under Ubuntu:
sudo apt-get install llvm
sudo apt-get install clang
sudo apt-get install llvm-gcc-4.6
% llvm-g++ -emit-llvm x.cpp -o program.bc -c
% llc -march=c program.bc -o x.c
is error: llc: program.bc:1:1: error: expected top-level entity
also "lli program.bc" is error 

0
borneq1 (31)
12/7/2012 7:35:12 PM
On 12/07/2012 02:35 PM, Borneq wrote:
> Uzytkownik "Keith Thompson" <kst-u@mib.org> napisal w wiadomosci 
> news:ln38zhptp2.fsf@nuthaus.mib.org...
>> We can probably give you a better answer if you let us know *why*
>> you want to translate the C++ code to C.  How does leaving the
>> code in C++ and compiling it with a C++ compiler not satisfy your
>> requirements?
> 
> I wanna for training reasons write compiler (rightly translator) from my own 
> object language to C+asm.

How does that involve translation of C++ code to C?

> ... It will be well if it support exceptions and 
> multiple Inheritance.

Well, C has neither exceptions nor inheritance of any kind, much less
multiple inheritance. You might need to reconsider your plans. For
instance, why don't you target C++ + asm, rather than C + asm?

> sudo apt-get install llvm
> sudo apt-get install clang
> sudo apt-get install llvm-gcc-4.6
> % llvm-g++ -emit-llvm x.cpp -o program.bc -c

So, where did x.cpp come from, and what does it have to to with the
situation you described above?

> % llc -march=c program.bc -o x.c
> is error: llc: program.bc:1:1: error: expected top-level entity
> also "lli program.bc" is error 

If you want to know why you got that error message, a forum devoted to
llvm would be a better place to find out. You might need to explain the
contents of x.cpp in order to get a meaningful answer.
0
jameskuyper (5635)
12/7/2012 8:15:31 PM
On Fri, 7 Dec 2012 16:40:28 +0100, "Borneq"
<borneq@antyspam.hidden.pl> wrote:

>U?ytkownik "James Kuyper" <jameskuyper@verizon.net> napisa? w wiadomo?ci 
>news:k9spjv$8cb$1@dont-email.me...
>> On the other hand: if your entire program never actually handles
>> exceptions, then you may be able to adequately emulate exceptions simply
>> by replacing throw() with exit(). That's the case for the C++ code I'm
>> working on right now.
>
>Is http://www.microsoft.com/msj/0197/Exception/Exception.aspx with code:
>int main()
>{
>    DWORD handler = (DWORD)_except_handler;
>
>    __asm
>    {                           // Build EXCEPTION_REGISTRATION record:
>        push    handler         // Address of handler function
>        push    FS:[0]          // Address of previous handler
>        mov     FS:[0],ESP      // Install new EXECEPTION_REGISTRATION

so here FS:[0] point to a region of mem that has in its first 4bytes
the address of the *previous* handler; right?

>    }
>
>    __asm
>    {
>        mov     eax,0           // Zero out EAX
>        mov     [eax], 1        // Write to EAX to deliberately cause a 
>fault
>    }
>
>    printf( "After writing!\n" );
>
>    __asm
>    {                           // Remove our EXECEPTION_REGISTRATION record
>        mov     eax,[ESP]       // Get pointer to previous record
>        mov     FS:[0], EAX     // Install previous record
>        add     esp, 8          // Clean our EXECEPTION_REGISTRATION off 
>stack
>    }
>
>    return 0;
>}
>

what is one handler?
if it is one function, what arg it has? what it return?

this here crash...
__________
;nasmw -fobj  that.asm
section _DATA use32 public class=DATA

global _fini
global _fend

dat dd 0, 0, 0, 0

section _TEXT use32 public class=CODE

; 0ra, 4P_address_hadler_function
_fini:    
          mov     edx,  dword[esp+  4]
          cmp     edx,  0
          jne     .1
..e:       mov     eax,  -1
          stc
          jmp     short  .z
..1:       mov     eax,  [fs:0]
          cmp     eax,  0
          je      .e
          mov     [dat],  eax   ; store address of previos hf
          mov     [dat+4],  edx ; store address of         hf
          mov     ecx,  dat
          mov     [fs:0],  ecx
          xor     eax,  eax
..z:       
          ret

_fend:    
          mov     eax,  [dat]
          cmp     eax,  0
          je      .z
          mov     [fs:0],  eax
..z:       
          ret
__________
/*bcc32 -v this.c  that.obj */
#include <stdio.h>

int fini(char*  a);
int fend(void);
int except_handler(void){return 1;}  

int main()
{unsigned  a,b;

 fini(except_handler);
 a=0; b=9; b=b/a; 
 printf( "After writing! %u\n", b);

 fend();

 return 0;

}

0
Rosario9591 (197)
12/7/2012 8:24:16 PM
On 12/7/2012 12:44 PM, Ian Collins wrote:
> BGB wrote:
>> On 12/7/2012 2:41 AM, Ian Collins wrote:
>>> Borneq wrote:
>>>> Użytkownik "Ian Collins" <ian-news@hotmail.com> napisał w wiadomości
>>>> news:aidkuhFac77U1@mid.individual.net...
>>>>> Start from scratch.
>>>>
>>>> How start? I haven't information about exception handling in C
>>>
>>> There isn't any, you have to rewrite the code from scratch.
>>>
>>
>> well, it is possible, either to use OS-specific mechanisms (like Win32
>> SEH), or write ones' own exception-handling system (potentially
>> involving linked-lists and longjmp), and rewrite any code to use this.
>>
>> it is kind-of ugly, but it sort of works...
>
> It would also be fraught with danger.  Writing exception safe code in C
> isn't easy.
>

yes, but it can be done...

granted, using it is partly more about consistency though.


in my case, I have a mechanism which works in one of 2 ways:
on Win32, it hooks into SEH, and uses a special exception code;
on other targets (such as Linux), it uses TLS, linked lists, and 
longjmp, basically to implement a sort of "fake SEH".

the external interface sort of crudely implements a system vaguely like 
try/catch/finally in Java, just based around function calls (and if/else 
blocks function-calls are basically used to implement the 
try/catch/finally blocks).

granted, yes, it is kind of ugly and painful to use, and so for the most 
part I ended up sticking to using return values to indicate error status.


when it came to the choice of whether my scripting language should throw 
exceptions in C land for uncaught script exceptions, or just return an 
error status, I ended up opting for an error status (with another API 
call to optionally fetch the VM's exception object).

note that if the exception manages to unwind all the way without getting 
caught anywhere, Win32 will give the message box and terminate the 
program. IIRC, for the fake-SEH, it is rigged up to print an error 
message and call "abort()" or similar.


or such...

0
cr88192355 (1928)
12/7/2012 11:26:49 PM
Użytkownik "Ian Collins" <ian-news@hotmail.com> napisał w wiadomości 
news:aidodaFac77U2@mid.individual.net...
>> Online http://www.comeaucomputing.com/tryitout/ can translate to C or 
>> only
>> to assembler?
>
> Neither.  You have to buy the compiler.

Is problem - I need both Windows and Linux compiler, comeau sold only one, 
two compilers will be too expensive 

0
borneq1 (31)
12/8/2012 10:08:05 AM

"christian.bau" <christian.bau@cbau.wanadoo.co.uk> wrote in message 
news:71d8f792-698f-4740-9841-4a587eb00f91@u19g2000yqj.googlegroups.com...
> There are situations where it doesn't make sense to say "how do I do
> this C++ thing in C"; the proper answer would be "just do it in C++".
> Exception handling is one of those things.

Yet C++ was originally implemented in C?

> When an exception is thrown, you have to clean up resources as the
> exception winds its way through the call chain.

What sort of resources need to be cleaned up?

Local objects that would have been deallocated on a normal return are 
understood. But what about more global/more persistent changes, where it's 
not clear what the intentions of the intermediate function would have been?

> In Java or C++ there
> are things in the language that do this clean up automatically.
> Without that, it is impossible for a mere human to always do that
> cleanup correctly. Forget it.

It sounds like this C code will be created automatically, so there is plenty 
of scope to add in all this capability, especially if performance isn't 
important.

-- 
Bartc 

0
bc (2337)
12/8/2012 12:46:29 PM
On 12/08/2012 07:46 AM, BartC wrote:
> 
> 
> "christian.bau" <christian.bau@cbau.wanadoo.co.uk> wrote in message 
> news:71d8f792-698f-4740-9841-4a587eb00f91@u19g2000yqj.googlegroups.com...
....
>> When an exception is thrown, you have to clean up resources as the
>> exception winds its way through the call chain.
> 
> What sort of resources need to be cleaned up?

The resources, if any, associated with objects with automatic storage
duration whose lifetime is ended by the throwing of the exception, or
which are released in catch{} blocks that match the thrown exception.

> Local objects that would have been deallocated on a normal return are 
> understood. But what about more global/more persistent changes, where it's 
> not clear what the intentions of the intermediate function would have been?

The "intentions" of the intermediate function are, in C++, expressed in
two ways: the destructors of the objects that are local to that
function, and the code in the catch{} blocks that correspond to the
thrown exception. It's more than a little difficult to do that in C,
which has neither destructors nor catch{} blocks.
-- 
James Kuyper
0
jameskuyper (5635)
12/8/2012 2:35:24 PM
Uzytkownik "James Kuyper" <jameskuyper@verizon.net> napisal w wiadomosci 
news:50C24E63.2090607@verizon.net...
> How does that involve translation of C++ code to C?

> Well, C has neither exceptions nor inheritance of any kind, much less
> multiple inheritance. You might need to reconsider your plans. For
> instance, why don't you target C++ + asm, rather than C + asm?

I compile with http://llvm.org/demo/index.cgi
and it call:
__cxa_allocate_exception
__cxa_throw
__cxa_begin_catch
__cxa_end_catch
how it call in own program? 

0
borneq1 (31)
12/8/2012 3:49:10 PM
On 12/08/2012 10:49 AM, Borneq wrote:
> Uzytkownik "James Kuyper" <jameskuyper@verizon.net> napisal w wiadomosci 
> news:50C24E63.2090607@verizon.net...
>> How does that involve translation of C++ code to C?
> 
>> Well, C has neither exceptions nor inheritance of any kind, much less
>> multiple inheritance. You might need to reconsider your plans. For
>> instance, why don't you target C++ + asm, rather than C + asm?
> 
> I compile with http://llvm.org/demo/index.cgi
> and it call:
> __cxa_allocate_exception
> __cxa_throw
> __cxa_begin_catch
> __cxa_end_catch
> how it call in own program? 

I've found documentation for those functions at
<http://libcxxabi.llvm.org/spec.html>, though I didn't dig into the
documentation in any depth. However, given the way that they're nbamed
and the fact that they're identified as being part of libc++, I would
make two guesses:

1) you might not need to call those functions explicitly when compiling
for C++, the calls might be generated automatically by the compiler in
connection with throw() and catch() constructs.

2) it might not be safe to call them from C.

However, those are just guesses -  only someone actually familiar with
llvm could address those issue authoritatively. So ask your question in
a forum devoted to llvm.
-- 
James Kuyper
0
jameskuyper (5635)
12/8/2012 4:04:15 PM
On Sat, 8 Dec 2012 12:46:29 -0000, "BartC" <bc@freeuk.com> wrote:

>
>
>"christian.bau" <christian.bau@cbau.wanadoo.co.uk> wrote in message 
>news:71d8f792-698f-4740-9841-4a587eb00f91@u19g2000yqj.googlegroups.com...
>> There are situations where it doesn't make sense to say "how do I do
>> this C++ thing in C"; the proper answer would be "just do it in C++".
>> Exception handling is one of those things.
>
>Yet C++ was originally implemented in C?


Cfront (Stroustrup's original C++ to C translator) was abandoned when
it became obvious that adding exceptions (which were a fairly late
addition to C++), was near impossible.  So Cfront never supported
exceptions.
0
robertwessel2 (1674)
12/8/2012 6:08:57 PM
On Friday, December 7, 2012 6:50:09 AM UTC, Borneq wrote:
> How can translate C++ to C code with exceptions handling? Unfortunately  
> Stroustrup translator cfron has only source and not compiled, llvm command 
> to translate C++ => C not works.
>
You mean hand translation?

Translate a throw to a goto error_exit.
In the error_exit portion of the code, free up everything you allocated 
in the function (so be sure not to have dangling uninitialised pointers).#
Then set a global "exception" flag.
After every call to a function which can throw, add If(expeption) goto error_exit;

To implement catch, check the exeception flag, then clear it. You'll have to
have jumps into catch.

It's not nice. But it will work without too much violence to your C code.

You have to be more sophisticated with automatic translation, because you 
have to handle every legal C++ construct.

0
12/8/2012 6:34:08 PM
On Sat, 2012-12-08, Borneq wrote:
> U??ytkownik "Ian Collins" <ian-news@hotmail.com> napisa?? w wiadomo??ci 
> news:aidodaFac77U2@mid.individual.net...
>>> Online http://www.comeaucomputing.com/tryitout/ can translate to C or 
>>> only
>>> to assembler?
>>
>> Neither.  You have to buy the compiler.
>
> Is problem - I need both Windows and Linux compiler, comeau sold only one, 
> two compilers will be too expensive 

Why do you want pure C, anyway?  If what you have now is C++, and you
want a typical C++ feature like exceptions, why not continue with C++?

Some people avoid C++ because the language is more complex than C, but
C code created by automatic translation from C++ (not to mention the
exception thing) will be *far worse* than either C or C++.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .
0
nntp24 (1801)
12/8/2012 7:02:03 PM
On Sat, 2012-12-08, Jorgen Grahn wrote:
> On Sat, 2012-12-08, Borneq wrote:
>> U??ytkownik "Ian Collins" <ian-news@hotmail.com> napisa?? w wiadomo??ci 
>> news:aidodaFac77U2@mid.individual.net...
>>>> Online http://www.comeaucomputing.com/tryitout/ can translate to C or 
>>>> only
>>>> to assembler?
>>>
>>> Neither.  You have to buy the compiler.
>>
>> Is problem - I need both Windows and Linux compiler, comeau sold only one, 
>> two compilers will be too expensive 
>
> Why do you want pure C, anyway? [...]

Never mind; it was explained (sort of) elsewhere in the thread.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .
0
nntp24 (1801)
12/8/2012 7:22:05 PM
On 12/8/2012 12:08 PM, Robert Wessel wrote:
> On Sat, 8 Dec 2012 12:46:29 -0000, "BartC" <bc@freeuk.com> wrote:
>
>>
>>
>> "christian.bau" <christian.bau@cbau.wanadoo.co.uk> wrote in message
>> news:71d8f792-698f-4740-9841-4a587eb00f91@u19g2000yqj.googlegroups.com...
>>> There are situations where it doesn't make sense to say "how do I do
>>> this C++ thing in C"; the proper answer would be "just do it in C++".
>>> Exception handling is one of those things.
>>
>> Yet C++ was originally implemented in C?
>
>
> Cfront (Stroustrup's original C++ to C translator) was abandoned when
> it became obvious that adding exceptions (which were a fairly late
> addition to C++), was near impossible.  So Cfront never supported
> exceptions.
>

and, apparently no one considered doing it like something like this:

if(ehBeginTry())
{
	... do stuff ...
	ehEndTry();
}else if(ehCatch("SomeException"))
{
	... handle stuff ...
	ehEndCatch();
}else
....
}else if(ehBeginFinally())
{
	... cleanup ...
	ehEndFinally();		//continues unwinding
}


where "ehBeginTry()" would register the exception-handler, and 
"ehEndTry()" would unregister it.

the "ehBeginTry()" return value will indicate whether they are entering 
the "try" block, or handling a thrown exception.


in cases where resources need to be cleaned up, a block can be written like:
if(ehBeginTry())
{
	... do stuff ...
	ehEndTry();
}else if(ehBeginFinally())
{
	... cleanup ...
	ehEndFinally();
}



or such...

0
cr88192355 (1928)
12/8/2012 9:19:31 PM
On 08.12.2012 16:49, Borneq wrote:

>> Well, C has neither exceptions nor inheritance of any kind, much less
>> multiple inheritance. You might need to reconsider your plans. For
>> instance, why don't you target C++ + asm, rather than C + asm?
>
> I compile with http://llvm.org/demo/index.cgi
> and it call:
> __cxa_allocate_exception
> __cxa_throw
> __cxa_begin_catch
> __cxa_end_catch
> how it call in own program?

You don't. You write C++ in first place. Look, the way *how* a C++ 
compiler implements exceptions is defined by the specific compiler 
implementation. G++ for example does not generate much code, instead it 
creates a table (within the text segment, i.e. same place where the code 
goes) which defines which objects to clean up. This table is then parsed 
by the exception handling mechanism.

What you try to do is not feasible - you can "simulate" *some* of the 
features of exceptions in C, but not all. The troublesome part is that 
you need to call the destructors of all automatic objects when 
propagating an exception out of a function.

If your C++ code is written in such a way that all automatic objects 
have trivial destructors, one can somehow emulate exception handling by 
setjmp/longjmp (been there, done that), but given that you have already 
an existing C++ code - no way.

There are free C++ compilers out there, for example the mentioned GNU 
g++. Just use it - problem solved.

Greetings,
	Thomas
0
thor16 (336)
12/9/2012 2:05:04 PM
On 12/8/12 4:46 AM, BartC wrote:
> Yet C++ was originally implemented in C?

Exceptions were one of the main reasons why C++ stopped being
implemented as a front-end to C.  IIRC, 'D&E' by Stroustrup mentions this.

-- 
Jonathan Leffler                   #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2011.0612 -- http://dbi.perl.org/
0
jleffler (73)
12/17/2012 6:37:42 AM
Reply:

Similar Artilces:

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta > * open source - preferable, but not 100% necessary what's wrong with using C? Friedrich > what's wrong with using C? Too low level, no built-in garbage collection. -- Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: >> what's wrong with using C? > > Too low level, Well the Smalltalk people say, have a small language and a big library. I assume C's library status beyond that of Smalltalk. > no built-in garbage collection. Use the Boehm Weisser GC. Friedrich "Kevin Albrecht" <kevin@albrecht.net> wrote in message news:<2TiYb.237892$rj7.1641209@twister.tampabay.rr.com>... > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) > ...

Exceptions in C/C++
Hello, I have question concerning such code: __try { ... } __except(EXCEPTION_EXECUTE_HANDLER) { printf("Exception code: %.8x\n", GetExceptionCode()); } Is C related code? Or maybe C++? opexoc opexoc@gmail.com wrote: > Hello, > > I have question concerning such code: > > __try > { > ... > } __except(EXCEPTION_EXECUTE_HANDLER) > { > printf("Exception code: %.8x\n", GetExceptionCode()); > } > > > Is C related code? Or maybe C++? It's not ISO C, but Microsoft specific C, see their MSDN doc. -- Tor <b...

C/C++ exceptions
Hi, I'm using setjmp/longjmp to do C++-like exceptions in C. As long as i catch all longjmps within a C function, is it ok to link the code into a C++ program without conflicts with the C++ exceptions and other things? On Fri, 06 May 2005 03:12:46 +1000, Russell Shaw wrote: > Hi, > I'm using setjmp/longjmp to do C++-like exceptions in C. As long as i > catch all longjmps within a C function, is it ok to link the code into a > C++ program without conflicts with the C++ exceptions and other things? Exceptions, even when propagating trough C code, will work, so no problem t...

slime C-c C-c annoyance
Does anyone have a good workaround for this problem other than: just be careful! When I've compiled a large package and slime has given me a *slime-compilation* buffer, I like to work through this buffer issue by issue, fixing the errors, warnings, etc. But If I ever press C-c C-c in the editor window while fixing an error, slime destroys the *(slime-compilation* buffer with the compilation output of C-c C-c. Jim Newton <jimka.issy@gmail.com> writes: > Does anyone have a good workaround for this problem other than: just be careful! > > When I've compiled a ...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

c,c++,obj-c
Of the 3 languages titled in this post is c++ older than obj-c ? Would someone experienced in C be able to dive right into C++ rather than obj-c ? Java is out for me now except applet programming I think I'm going to go with just C and C++. Bill Bill Cunningham wrote: > Of the 3 languages titled in this post is c++ older than obj-c ? Why does that matter? Do you want to use an old language? Try Fortran or LISP. > Would > someone experienced in C be able to dive right into C++ rather than > obj-c ? Not quite dive in, at least not into the OO parts of C++. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! In article <1120963701.862698.160260@g14g2000cwa.googlegroups.com>, Throne Software <mail@thronesoftware.com> writes >Throne Software has opened up C, C++ and C# Forums at: > >http://www.thronesoftware.com/forum/ > >Join us! Why? The beauty of news groups is that they come to me I don't have to go to them. Also all the people who have been around a while know what they are doing and are here. I noticed that there are some C and C++ Forums on another system. Full of the blind (opinionated) leading the blind. The problem is that as the experienced people who have been around a long while are on usenet. the system works. Why move. Certainly not to a forum on a commercial web site. The web site might go down. Usenet can't. Web based systems are not as safe. I have to trust you about cookies and all sorts. (active X etc). Usenet is international and independent. It is not controlled by anyone. Why would anyone move from this system to a web based one in a a rogue state? There is no point in running a web based forum except as a support forum for your own products. -- \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/ /\/\/ chris@phaedsys.org www.phaedsys.org \/\/\ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Chris Hills wrote: > In article <1120963701.862...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! ...

C,, C++, Java, C#
I come from C,C++,Java and C#. What can SmallTalk do for me and for what cost? Regarding the Squeak thing, it's interesting though confusing. Is it an IDE? Why are the menus so wacky? "DM McGowan II" <nospam@nospam.net> wrote in message news:PLednX2vnLd11IncRVn-rA@comcast.com... > I come from C,C++,Java and C#. What can SmallTalk do for me ...? Ask not what Smalltalk can do for, but what can you do with Smalltalk. :-) In very few words, it significantly reduces time to market, while enhancig the development experience. This is because: 1. It manages the memory for you, so you don't have to allocate/deallocate all the time (the source of some of the most nasty bugs in C). Java and C# learnt that feature from ST. 2. It has single inheritance. 3. All the development is in a single file, called the image, so you don't have to fish for where did you put that missing class. Yet, you don't have to go through thousands of lines of code to find the method you wrote three months ago. 4. It's the language for which it's easier to work within the Agile methodologies. 5. It's strongly typed without being unnecessarily redundant. 6. It's "wacky" interface releases you from the need to use curlys. 7. It comes with a huge library, so you don't need to reinvent the wheel as much. 8. It's debugger is highly integrated and friendly, so the experience of findin...

c++ or c#
hello all experiancesd techies, i am a final yr engg student with knowledge of c++ n java. i want to start windows programing, n hence have to decide whether to choose my language as c++ or c#. please guide me which language would benefit in the long run, if i intend to persue a job in a product development company/( like adobe) after graduation,(not in a consultancy like acenture). also suggest me good books for that. thanx a lot <pravin.chaudhary@gmail.com> wrote in message news:1131971088.291580.42500@g47g2000cwa.googlegroups.com... > i am a final yr engg student with ...

C++ or C?
I am new to MPI and have a basic understanding of C and C++. So I was wondering if any of you guys with experience in using MPI had any pointers with regards choosing either C or C++. I intend to use MPI in parallelizing code I have that was written in Java. So obviously the Java code it is heavly object orientated. With regards parallelizing it; right now I think it will mainly be a Master-Slave style of design with no inter-process communication needed once the master has sent the work to the slave. Most, if not all, of my communications will involve simply sending a large object from the master to the slaves, with the slaves returning this object (a modified version of) to the master. Right now I am thinking that it would be most convenient to just write it in C++. But with regards performance, will there be a significant overhead in using C++ as oppossed to C. Also, can I simply send a C++ object using MPI_Send? Apologies if these questions sound naive/trivial...but i am new :) Any help appreciated, Pat. Patrick schrieb: > I am new to MPI and have a basic understanding of C and C++. So I was > wondering if any of you guys with experience in using MPI had any > pointers with regards choosing either C or C++. > > I intend to use MPI in parallelizing code I have that was written in > Java. So obviously the Java code it is heavly object orientated. With > regards parallelizing it; right now I think it will mainly be a > Master-Slave style o...

c,c++
hi how r u i m joining ur group faheem.jmi001@gmail.com wrote: > hi how r u i m joining ur group I think you are looking for the remedial written english class. That's running in the annex... Mark Bluemel wrote: > faheem.jmi001@gmail.com wrote: >> hi how r u i m joining ur group > > I think you are looking for the remedial written english class. That's > running in the annex... Such a warm welcome for a cold night. -- Ian Collins. In article <1189676628.511881.124390@d55g2000hsg.googlegroups.com>, faheem.jmi001@gmail.com writes >hi how r u i ...

c to c++
hi, i was wondering whether its worth converting from c to c++? i'm lazy but if its worth the effort... ______________________ http://monsterbeans.com/ | --------------------------------------- eminhanif@googlemail.com wrote: > hi, > i was wondering whether its worth converting from c to c++? > i'm lazy but if its worth the effort... Rather than participating in the language war flamefest your message invites, I will simply make the most obvious point: it is worth your while to learn any language that will be used in connection with your work. This includes learning a ...

More about C and C++
Hello... I have wrote: >I will resume it like this: >On the criterias of "complexity" and "difficulty", the C and C++ >programming languages are more complex and difficult than Object >Pascal for example, so since they are more complex and difficult they >are likely to create something like a darwinian filter who don't let >the weaker humans among us humans to cross or to climb the social >ladder, this is why i think that C and C++ do participate to social >darwinism, this is why i say that C and C++ are bad. Josep...

Question about mixing c++ exception handling & c code
Hi, I was looking thru code in my organisation. same dll is sharingcode is mixing c++ code & c code, which is being again called by some other c+ + dll. Only issue I am not too sure about exception handling in this case - here is trimmed code snippet - include.h typedef void (func*)(void *,bool); struct scriptparams { func callback; void *obj; }; test.cpp class execption { public: virtual void printerror(){//do something}; }; class foo { public: foo(){}; void dosomething(bool arg) { cout>>"do something">>endl; if(!arg) throw execption; //if valid do someth...

About C and C++
Hello... I will resume it like this: On the criterias of "complexity" and "difficulty", the C and C++ programming languages are more complex and difficult than Object Pascal for example, so since they are more complex and difficult they are likely to create something like a darwinian filter who don't let the weaker humans among us humans to cross or to climb the social ladder, this is why i think that C and C++ do participate to social darwinism, this is why i say that C and C++ are bad. Thank you, Amine Moulay Ramdane. On Sunday, 8 June 2014...

c++ or c#
Hi. I'm new to c++ and still learning it. I've been advised that c and c++ are now becoming obsolete..I better learn c# (supposedly the best language available) ..is it true?? * sandy: > Hi. I'm new to c++ and still learning it. I've been advised that c > and c++ are now becoming obsolete..I better learn c# (supposedly the > best language available) ..is it true?? The premise is false but many, including me, agree with the conclusion that you should learn e.g. C# first (obviously for different reasons). Learning C# or another language first might take you months o...

C and C
Please Who wants to post Command and Conquer Red Alert 3. I'm looking for the whole version. If you do so Thank you. --------------= Posted using GrabIt =---------------- ------= Binary Usenet downloading made easy =--------- -= Get GrabIt for free from http://www.shemes.com/ =- Shaniel's Usenet wrote: > Please > Who wants to post Command and Conquer Red Alert 3. > I'm looking for the whole version. > If you do so Thank you. > > > --------------= Posted using GrabIt =---------------- > ------= Binary Usenet downloading made easy =--------- &g...

C or C++?
Hi, I am a newbie to programming, and sorry again that I have to ask the C or C++ questions. Is C required before learning C++? And become better in C does it also make you a better C++ programmer? Or that to be a C+ + programmer, it's better not to have any knowledge of C and start a new in the C++ way as some books suggest? weidongtom@gmail.com wrote: > Hi, > > I am a newbie to programming, and sorry again that I have to ask the C > or C++ questions. Is C required before learning C++? No. And become better > in C does it also make you a better C++ programmer? Not n...

More about C and C++
Hello, I have wrote: >I will resume it like this: >On the criterias of "complexity" and "difficulty", the C and C++ >programming languages are more complex and difficult than Object >Pascal for example, so since they are more complex and difficult they >are likely to create something like a darwinian filter who don't let >the weaker humans among us humans to cross or to climb the social >ladder, this is why i think that C and C++ do participate to social >darwinism, this is why i say that C and C++ are bad. Josep...

Question about mixing c++ exception handling & c code
Hi, I was looking thru code in my organisation. same dll is sharingcode is mixing c++ code & c code, which is being again called by some other c+ + dll. Only issue I am not too sure about exception handling in this case - here is trimmed code snippet - include.h typedef void (func*)(void *,bool); struct scriptparams { func callback; void *obj; }; test.cpp class execption { public: virtual void printerror(){//do something}; }; class foo { public: foo(){}; void dosomething(bool arg) { cout>>"do something">>endl; if(!arg) throw execption; //if valid do someth...

C++ or C?
somebody told me that c++ is superior to c while others insist that c++ is just a junk. I don't know which to follow.I want to know exactly the difference between them, and their respective advantage and disadvantage. Magcialking wrote: > somebody told me that c++ is superior to c while others insist that > c++ is just a junk. > I don't know which to follow.I want to know exactly the difference > between them, and their respective advantage and disadvantage. www.google.com and read about the differences until you're blue in the face. Oh, while you're at it, loo...

What After C and C++?
First it was C, then C++, what comes next? "Penna Elabi" <terebinthus@go.com> wrote in message news:77081966.0311290846.3d5a7064@posting.google.com... > First it was A whole bunch of computer languages, then >C, then a whole bunch more computer languages, then >C++, what comes next? A whole bunch more. So what? Did you have a question about C++ (which is the *only* topic here) ? -Mike "Penna Elabi" <terebinthus@go.com> wrote... > First it was C, then C++, what comes next? Who cares? This newsgroup is concerned with C++, not with what c...

Web resources about - Exceptions handling in pure C - comp.lang.c

Motor vehicle exception - Wikipedia, the free encyclopedia
The exception is based on the idea that there is a lower expectation of privacy in motor vehicles due to the regulations they operate. Additionally, ...

Podcast #42 – It’s The Exception That Proves The Rule
... Language and Usage , which was frustrated by the high number of certain types of questions that were coming in. Is this podcast the exception ...

Santorum: No Rape or Incest Exceptions - YouTube
Santorum: No Rape or Incest Exceptions - YouTube

Can Tunisia, the 'Arab exception', hold on to democractic ideals amid turmoil?
The terrorist attack on the Bardo Museum, in Tunis, represents yet another episode of senseless, barbaric violence in the name of Islam.

Exception to Origin rule
Exception to Origin rule

Peter FitzSimons: Sydney siege met with courage, wisdom and one exception
Senator David Leyonhjelm's response to the Sydney siege was gobsmacking.

Remakes are all the rage, and sport's no exception
Over the past year or so, franchises such as Star Wars and Rocky have solidified what many pundits already believed, and that is that people ...

Most consumers better off after carbon tax scrapped – with one possible exception
Households are roughly $550 a year better off since the scraping of the carbon tax, but air travellers may wonder if they are being overcharged, ...

Men Need to Admit That Jian Ghomeshi Is No Exception
That men like Jian Ghomeshi are a terrible exception and if we as men simply stand up and say we are not as bad as that, or that we are not rapists ...

No exception in punishing the corrupt: People's Daily
No exception in punishing the corrupt: People's Daily People's Daily Online BEIJING, June 30 People's Daily , the flagship newspaper of the ...

Resources last updated: 3/10/2016 7:08:08 PM