Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need experts opnion

Reply
Thread Tools

Need experts opnion

 
 
vashwath@rediffmail.com
Guest
Posts: n/a
 
      01-05-2006
Hi all,
In my current project I am thinking to use setjmp/lngjmp for exception
handling.The way I am planing to do this is shown in the below example.
Please if this is the right way to do.Are there any disadvantages in
this method?

#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf g_env;
extern void fun1();
int main()
{
int ret;

ret = setjmp(g_env);
if (ret == 0)
{
printf("Perform the intended functionality");
fun1();
exit(EXIT_SUCCESS);
}
else
{
printf("Exception handling\n");
exit(EXIT_FAILURE);
}

}


#include <stdio.h>
#include <setjmp.h>

extern jmp_buf g_env;

void fun1()
{
longjmp(g_env,1);
}

 
Reply With Quote
 
 
 
 
tmp123
Guest
Posts: n/a
 
      01-05-2006
wrote:
> Hi all,
> In my current project I am thinking to use setjmp/lngjmp for exception
> handling.The way I am planing to do this is shown in the below example.
> Please if this is the right way to do.Are there any disadvantages in
> this method?
>
> #include <stdio.h>
> #include <setjmp.h>
> #include <stdlib.h>
> jmp_buf g_env;
> extern void fun1();
> int main()
> {
> int ret;
>
> ret = setjmp(g_env);
> if (ret == 0)
> {
> printf("Perform the intended functionality");
> fun1();
> exit(EXIT_SUCCESS);
> }
> else
> {
> printf("Exception handling\n");
> exit(EXIT_FAILURE);
> }
>
> }
>
>
> #include <stdio.h>
> #include <setjmp.h>
>
> extern jmp_buf g_env;
>
> void fun1()
> {
> longjmp(g_env,1);
> }




Hi,

Starting with the code you has post, there are several posible
improvements:

1) Do not use a single jmp_buf, but an array of it.
2) Define macros to made more visible the exception handling. Only as
an example:

#define TRY if (setjmp(g_env))
#define CATCH else
#define THROW(x) longjmp(g_env,x)

mades the code

TRY
{
}
CATCH
{
}

....


THROW(1)

3) Do not forget the falg volatile when necessary.

Kind regards.

 
Reply With Quote
 
 
 
 
vashwath@rediffmail.com
Guest
Posts: n/a
 
      01-05-2006

tmp123 wrote:
> 1) Do not use a single jmp_buf, but an array of it.


Could you please explain in detail why jmp_buf should be an array?

 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-05-2006
wrote:
>
> In my current project I am thinking to use setjmp/lngjmp for
> exception handling.The way I am planing to do this is shown in
> the below example. Please if this is the right way to do.Are
> there any disadvantages in this method?
>

*** quote munged to reduce vertical space ***
> #include <stdio.h>
> #include <setjmp.h>
> #include <stdlib.h>
> jmp_buf g_env;
> extern void fun1();
> int main()
> {
> int ret;
>
> ret = setjmp(g_env);
> if (ret == 0) {
> printf("Perform the intended functionality");
> fun1();
> exit(EXIT_SUCCESS);
> }
> else {
> printf("Exception handling\n");
> exit(EXIT_FAILURE);
> }
> }
>
> #include <stdio.h>
> #include <setjmp.h>
>
> extern jmp_buf g_env;
>
> void fun1()
> {
> longjmp(g_env,1);
> }


Surprise. Your very first statement "ret = setjmp(...)" is
illegal! The following is a quote from N869:

Environmental limits

[#4] An invocation of the setjmp macro shall appear only in
one of the following contexts:

-- the entire controlling expression of a selection or
iteration statement;

-- one operand of a relational or equality operator with
the other operand an integer constant expression, with
the resulting expression being the entire controlling
expression of a selection or iteration statement;

-- the operand of a unary ! operator with the resulting
expression being the entire controlling expression of a
selection or iteration statement; or

-- the entire expression of an expression statement
(possibly cast to void).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
Reply With Quote
 
vashwath@rediffmail.com
Guest
Posts: n/a
 
      01-05-2006

Chuck F. wrote:
> Surprise. Your very first statement "ret = setjmp(...)" is
> illegal! The following is a quote from N869:
>
> Environmental limits
>
> [#4] An invocation of the setjmp macro shall appear only in
> one of the following contexts:
>
> -- the entire controlling expression of a selection or
> iteration statement;
>
> -- one operand of a relational or equality operator with
> the other operand an integer constant expression, with
> the resulting expression being the entire controlling
> expression of a selection or iteration statement;
>
> -- the operand of a unary ! operator with the resulting
> expression being the entire controlling expression of a
> selection or iteration statement; or
>
> -- the entire expression of an expression statement
> (possibly cast to void).

I am not able to understand any of these points. Can anybody explain in
simpler way(if possible with an example)?
Thanks in Advance

 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      01-05-2006

wrote:
> tmp123 wrote:
> > 1) Do not use a single jmp_buf, but an array of it.

>
> Could you please explain in detail why jmp_buf should be an array?


If you have an array of similar, you can have nested TRYs, in same or
different procedures.

Kind regards.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-05-2006
wrote:
> Chuck F. wrote:
>> Surprise. Your very first statement "ret = setjmp(...)" is
>> illegal! The following is a quote from N869:
>>
>> Environmental limits
>>
>> [#4] An invocation of the setjmp macro shall appear only in
>> one of the following contexts:
>>
>> -- the entire controlling expression of a selection or
>> iteration statement;
>>
>> -- one operand of a relational or equality operator with
>> the other operand an integer constant expression, with
>> the resulting expression being the entire controlling
>> expression of a selection or iteration statement;
>>
>> -- the operand of a unary ! operator with the resulting
>> expression being the entire controlling expression of a
>> selection or iteration statement; or
>>
>> -- the entire expression of an expression statement
>> (possibly cast to void).

> I am not able to understand any of these points. Can anybody explain in
> simpler way(if possible with an example)?
> Thanks in Advance


You are not allowed to do something like
ret = setjmp(...);
You should do something like:
if (setjmp(...)...)
or:
if (!setjmp(...))
or:
switch (setjmp(...)) {
...
}
You can also just have a line:
setjmp(...);
I.e. not using the value returned.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      01-05-2006
In article < .com>,
wrote:

> Hi all,
> In my current project I am thinking to use setjmp/lngjmp for exception
> handling.The way I am planing to do this is shown in the below example.
> Please if this is the right way to do.Are there any disadvantages in
> this method?


One problem with any kind of exception handling is that exceptions
thrown will return to the exception handling code, skipping all function
calls in between. Any cleanup that is required by functions between the
initial caller and the place where the exception is thrown will not be
executed.

In Java, this problem is partially solved by having garbage collection.
Just organise your code so that throwing away dynamically allocated
objects will be enough to clean up. In C++, this problem is partially
solved by having destructors. Just organise your code so that
destructing any objects on the stack will be enough to clean up. In C,
there is no such thing. Any cleanup has to be done manually. You will
find yourself in major trouble very soon.

Unless you want to put superhuman effort into exception handling, the
only thing you can do in C is using setjmp/longjmp to cover complete
major subsystems; allocating all memory through functions that keep
track of allocation and free all memory when an exception happens,
installing function pointers for cleanup when necessary. This is a major
operation that needs experienced and diligent programmers.
 
Reply With Quote
 
vashwath@rediffmail.com
Guest
Posts: n/a
 
      01-06-2006

> In Java, this problem is partially solved by having garbage collection.
> Just organise your code so that throwing away dynamically allocated
> objects will be enough to clean up.


If I intialize all the pointers used for allocating memory to NULL and
call free on all these pointers uppon exception, then I have cleaned up
successfully is'nt it?

 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      01-06-2006
Christian Bau wrote:
> In article < .com>,
> wrote:
>
> > Hi all,
> > In my current project I am thinking to use setjmp/lngjmp for exception
> > handling.The way I am planing to do this is shown in the below example.
> > Please if this is the right way to do.Are there any disadvantages in
> > this method?

>
> One problem with any kind of exception handling is that exceptions
> thrown will return to the exception handling code, skipping all function
> calls in between. Any cleanup that is required by functions between the
> initial caller and the place where the exception is thrown will not be
> executed.
>
> In Java, this problem is partially solved by having garbage collection.
> Just organise your code so that throwing away dynamically allocated
> objects will be enough to clean up. In C++, this problem is partially
> solved by having destructors. Just organise your code so that
> destructing any objects on the stack will be enough to clean up. In C,
> there is no such thing. Any cleanup has to be done manually. You will
> find yourself in major trouble very soon.
>
> Unless you want to put superhuman effort into exception handling, the
> only thing you can do in C is using setjmp/longjmp to cover complete
> major subsystems; allocating all memory through functions that keep
> track of allocation and free all memory when an exception happens,
> installing function pointers for cleanup when necessary. This is a major
> operation that needs experienced and diligent programmers.



I agree on the previous (the reference to Java memory handling could be
completed with some security issues? and nested tries must be
referenced?).

However, the advantages of exceptions are also important. (Well, in
fact, nobody doubts today about benefits of exceptions, so, I'm being
redundant)

See two examples of a usual code with an without:

char * getLine( void )
{
FILE *f=NULL;
char *r=NULL;

if ( (r=malloc_string() == NULL ) return NULL;
if ( (f=file_open() == NULL )
{
/* Mixed messages and code!*/
fprintf(stderr, "unable to alloc...);
free(r);
return NULL;
}
if ( fgets(...) ... )
{
fprintf(stderr, "unable to ...);
free(r);
fclose(f); /* error handling grows! */
return NULL;
}
...
fclose(f);
return r;
}

char * getLines( const char *name )
{
FILE *f=NULL;
char *r=NULL;

TRY
{
f=file_open();
r=malloc_string();
if ( fgets(...) ... ) THROW(EXC_FAIL_READ);
...
fclose(f);
}
CATCH
{
if ( f != NULL ) fclose(f);
if ( r != NULL ) free(r);
r = NULL;
/* write errro messages or keep it in the main TRY */
THROW_NEXT;
}
END_TRY
return r;
}

 
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
Need help from digital backup experts - UDF & SP2 All Things Mopar Digital Photography 46 02-15-2006 06:54 AM
v.urgent,need experts help, cant sort the datagrid properly samir dsf ASP .Net 0 01-27-2006 01:46 AM
Calling all MCP/MCSE Experts, Need your Opinion Ben Computer Support 2 08-31-2004 05:00 AM
I need some help from Java experts Santa Java 2 01-08-2004 01:49 AM
Need help from designer experts: templated control AW ASP .Net 0 11-05-2003 04:26 PM



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