Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Exceptions handling in pure C (http://www.velocityreviews.com/forums/t955229-exceptions-handling-in-pure-c.html)

Borneq 12-07-2012 06:50 AM

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.


Ian Collins 12-07-2012 07:42 AM

Re: Exceptions handling in pure C
 
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

Borneq 12-07-2012 08:23 AM

Re: Exceptions handling in pure C
 
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?


Ian Collins 12-07-2012 08:41 AM

Re: Exceptions handling in pure C
 
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

BGB 12-07-2012 09:10 AM

Re: Exceptions handling in pure C
 
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.
>



James Kuyper 12-07-2012 01:06 PM

Re: Exceptions handling in pure C
 
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

Borneq 12-07-2012 03:40 PM

Re: Exceptions handling in pure C
 
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/Ex...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;
}



James Kuyper 12-07-2012 04:24 PM

Re: Exceptions handling in pure C
 
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/Ex...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.


Borneq 12-07-2012 05:02 PM

Re: Exceptions handling in C + assembler
 
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.


James Kuyper 12-07-2012 05:25 PM

Re: Exceptions handling in C + assembler
 
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.


All times are GMT. The time now is 11:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.