Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > return (i = 1, NULL)

Reply
Thread Tools

return (i = 1, NULL)

 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      08-27-2003

On Tue, 26 Aug 2003, William Ahern wrote:
>
> Kevin D. Quitt <(E-Mail Removed)> wrote:
> >>> return (my_errno = SOME_VALUE, NULL);

> >
> > Why not:
> >
> > my_errno = SOME_VALUE; return NULL;

>
> Because I usually use brevity and clarity like so:
>
> if (check_parameter)
> return (errno = EINVAL, NULL);
>
> Tho, this was the first time I had to return a null pointer rather
> than another error identifer like -1.
>
> The alternatives are:
>
> if (some_condition) {
> errno = EINVAL;
> return NULL;
> }


> or
>
> if (some_condition1 || some_condition2 || some_condition3 || ...) {
> errno = (some_condition)? ENAMETOOLONG : EINVAL
> return NULL;
> }


I think the simplest solution has been determined to be:

if (some_condition)
return (errno = EINVAL, (void *) NULL);

Still, I agree that this is almost certainly obfuscation. Have
you considered my /idiom du jour/,

if (some_condition1)
goto exit_failure1;
if (some_condition2)
goto exit_failure2;
...

exit_failure1:
errno = EINVAL;
return NULL;

exit_failure2:
errno = ENAMETOOLONG;
return NULL;


Very compact, and has the added bonus that all the "exit" code is in
one place. That means it's harder to miss a free(), and easier to
figure out all the possible return codes (which should be documented
anyway, of course, but still..).

HTH,
-Arthur

 
Reply With Quote
 
 
 
 
Samuel Barber
Guest
Posts: n/a
 
      08-27-2003
Ben Pfaff <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> http://www.velocityreviews.com/forums/(E-Mail Removed) (Samuel Barber) writes:
>
> > The last is by far the best fix, most importantly because (void*)0 is
> > not portable to C++.

>
> When writing C code, there is no reason to worry about what C++
> compilers will think of it. (Header files are a possible
> exception.)


C code is not neccessarily bad if it's not C++-compatible, but
C++-compatibility is certainly a valid point when considering the
merits of a C code fragment in the abstract. There are many reasons to
prefer C++ compatibility, all things being equal.

Sam
 
Reply With Quote
 
 
 
 
Greg Comeau
Guest
Posts: n/a
 
      08-27-2003
In article <(E-Mail Removed) >,
Samuel Barber <(E-Mail Removed)> wrote:
>Ben Pfaff <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>> (E-Mail Removed) (Samuel Barber) writes:
>>
>> > The last is by far the best fix, most importantly because (void*)0 is
>> > not portable to C++.

>>
>> When writing C code, there is no reason to worry about what C++
>> compilers will think of it. (Header files are a possible
>> exception.)

>
>C code is not neccessarily bad if it's not C++-compatible, but
>C++-compatibility is certainly a valid point when considering the
>merits of a C code fragment in the abstract. There are many reasons to
>prefer C++ compatibility, all things being equal.


I think Sam has a reasonable point. Of course, it need not
always be the case, but clearly many C programmers
do -- and should -- worry about what C++ compilers will think.
--
Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      08-27-2003
In article <newscache$uuv9kh$x97$(E-Mail Removed)>,
Kevin Easton <(E-Mail Removed)> wrote:
>Greg Comeau <(E-Mail Removed)> wrote:
>> In article <(E-Mail Removed) >,
>> Samuel Barber <(E-Mail Removed)> wrote:
>>>Ben Pfaff <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>>>> (E-Mail Removed) (Samuel Barber) writes:
>>>>
>>>> > The last is by far the best fix, most importantly because (void*)0 is
>>>> > not portable to C++.
>>>>
>>>> When writing C code, there is no reason to worry about what C++
>>>> compilers will think of it. (Header files are a possible
>>>> exception.)
>>>
>>>C code is not neccessarily bad if it's not C++-compatible, but
>>>C++-compatibility is certainly a valid point when considering the
>>>merits of a C code fragment in the abstract. There are many reasons to
>>>prefer C++ compatibility, all things being equal.

>>
>> I think Sam has a reasonable point. Of course, it need not
>> always be the case, but clearly many C programmers
>> do -- and should -- worry about what C++ compilers will think.

>
>Since C translation units can be linked into C++ programs, why can't
>they just compile their .c files with a C compiler, even when they're
>part of a C++ project?


They can. But to require everybody do so is not always
sufficient, just like not requiring them to do so isn't.
When/If an obvious gain can be made, it should be considered,
in context.
--
Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
William Ahern
Guest
Posts: n/a
 
      08-27-2003
Arthur J. O'Dwyer <(E-Mail Removed)> wrote:
> I think the simplest solution has been determined to be:
>
> if (some_condition)
> return (errno = EINVAL, (void *) NULL);
>
> Still, I agree that this is almost certainly obfuscation. Have
> you considered my /idiom du jour/,
>
> if (some_condition1)
> goto exit_failure1;
> if (some_condition2)
> goto exit_failure2;
> ...
>
> exit_failure1:
> errno = EINVAL;
> return NULL;
>
> exit_failure2:
> errno = ENAMETOOLONG;
> return NULL;
>
> Very compact, and has the added bonus that all the "exit" code is in
> one place. That means it's harder to miss a free(), and easier to
> figure out all the possible return codes (which should be documented
> anyway, of course, but still..).


yes i use that extensively, tho i usually limit it to a simple "goto
fail", otherwise things seem a little too cluttered to me (trying to juggle
multiple labels). instead i'll initialize the relevent values so that the
fail section knows what to clean up and what not to. and so i might do
something like:

if (cond) {
errno = EINVAL;
goto fail;
}

anyhoo, this has been an interesting thread, at least for myself.

thanx all.

[END OF THREAD]

 
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
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return &*i and return i; Ganesh Gella C++ 4 11-12-2004 04:28 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments