Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Exceptions handling in pure C

Reply
Thread Tools

Exceptions handling in pure C

 
 
Ian Collins
Guest
Posts: n/a
 
      12-07-2012
BGB wrote:
> On 12/7/2012 2:41 AM, Ian Collins wrote:
>> Borneq wrote:
>>> Użytkownik "Ian Collins" <ian-> napisał w wiadomości
>>> news:...
>>>> 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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-07-2012
"Borneq" <> 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- <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"
 
Reply With Quote
 
 
 
 
Borneq
Guest
Posts: n/a
 
      12-07-2012
Uzytkownik "Keith Thompson" <kst-> napisal w wiadomosci
news:...
> 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

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-07-2012
On 12/07/2012 02:35 PM, Borneq wrote:
> Uzytkownik "Keith Thompson" <kst-> napisal w wiadomosci
> news:...
>> 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.
 
Reply With Quote
 
Rosario1903
Guest
Posts: n/a
 
      12-07-2012
On Fri, 7 Dec 2012 16:40:28 +0100, "Borneq"
<> wrote:

>U?ytkownik "James Kuyper" <> napisa? w wiadomo?ci
>news:k9spjv$8cb$...
>> 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


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;

}

 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      12-07-2012
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-> napisał w wiadomości
>>>> news:...
>>>>> 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...

 
Reply With Quote
 
Borneq
Guest
Posts: n/a
 
      12-08-2012
Użytkownik "Ian Collins" <ian-> napisał w wiadomości
news:...
>> 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

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-08-2012


"christian.bau" <> wrote in message
news:71d8f792-698f-4740-9841-...
> 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

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-08-2012
On 12/08/2012 07:46 AM, BartC wrote:
>
>
> "christian.bau" <> wrote in message
> news:71d8f792-698f-4740-9841-...

....
>> 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
 
Reply With Quote
 
Borneq
Guest
Posts: n/a
 
      12-08-2012
Uzytkownik "James Kuyper" <> napisal w wiadomosci
news:...
> 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pure space directly inside div ignored, but pure space directlyinside span honored liketofindoutwhy@gmail.com HTML 4 03-29-2008 06:06 PM
Problem with exceptions, templates and pure virtual functions Dmitry Prokoptsev C++ 3 07-08-2006 07:58 PM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57