Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What's The Best Practice Defining Error Codes in C

Reply
Thread Tools

What's The Best Practice Defining Error Codes in C

 
 
Keith Thompson
Guest
Posts: n/a
 
      06-30-2012
Eric Sosman <(E-Mail Removed)> writes:
> On 6/30/2012 4:57 PM, rammy wrote:

[...]
>> I'm living in C++ world, [...]

>
> Then you're in the wrong newsgroup. comp.lang.c++ is just
> down the hall to your right, past the broom closet.


I thought it was past the water cooler.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-30-2012
On 6/30/2012 7:12 PM, Keith Thompson wrote:
> Eric Sosman <(E-Mail Removed)> writes:
>> On 6/30/2012 4:57 PM, rammy wrote:

> [...]
>>> I'm living in C++ world, [...]

>>
>> Then you're in the wrong newsgroup. comp.lang.c++ is just
>> down the hall to your right, past the broom closet.

>
> I thought it was past the water cooler.


You're right. Gosh, that thing sure needs fixing-up!
Ah, but that's C++ for you: All structure, no maintenance.

--
Eric Sosman
(E-Mail Removed)d


 
Reply With Quote
 
 
 
 
Maxim Fomin
Guest
Posts: n/a
 
      07-01-2012
воскресенье, 1 июля 2012*г., 1:34:07 UTC+4 пользователь Ian Collins написал:
> On 07/ 1/12 08:57 AM, rammy wrote:
>
> <snip>
>
> > I'm living in C++ world, so I want to try my best to make the API type
> > safe. Is there any way except enum could give a type safe error code
> > solution ?

>
> You will have to accept that from a C++ programmer's perspective, enums
> in C are somewhat broken due to automatic conversions. While C++ rules
> prevent silly mistakes like
>
> typedef enum { Good, Bad } Result;
>
> Result oops() { return -1; }
>
> C unfortunately does not. So from a type safety perspective, enums are
> no better than #defines.
>
> --
> Ian Collins


From what and to what are enumerations in C converted?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-01-2012
On 07/ 1/12 05:39 PM, Maxim Fomin wrote:
> воскресенье, 1 июля 2012 г., 1:34:07 UTC+4 пользователь Ian Collins написал:
>> On 07/ 1/12 08:57 AM, rammy wrote:
>>
>> <snip>
>>
>>> I'm living in C++ world, so I want to try my best to make the API type
>>> safe. Is there any way except enum could give a type safe error code
>>> solution ?

>>
>> You will have to accept that from a C++ programmer's perspective, enums
>> in C are somewhat broken due to automatic conversions. While C++ rules
>> prevent silly mistakes like
>>
>> typedef enum { Good, Bad } Result;
>>
>> Result oops() { return -1; }
>>
>> C unfortunately does not. So from a type safety perspective, enums are
>> no better than #defines.

>
> From what and to what are enumerations in C converted?


Anything that can be converted to or from an int, which includes other
enums.

--
Ian Collins
 
Reply With Quote
 
Maxim Fomin
Guest
Posts: n/a
 
      07-01-2012
воскресенье, 1 июля 2012*г., 11:05:22 UTC+4 пользователь Ian Collins написал:
> On 07/ 1/12 05:39 PM, Maxim Fomin wrote:
> > воскресенье, 1 июля 2012 г., 1:34:07 UTC+4 пользователь Ian Collins написал:
> >> On 07/ 1/12 08:57 AM, rammy wrote:
> >>
> >> <snip>
> >>
> >>> I'm living in C++ world, so I want to try my best to make the API type
> >>> safe. Is there any way except enum could give a type safe error code
> >>> solution ?
> >>
> >> You will have to accept that from a C++ programmer's perspective, enums
> >> in C are somewhat broken due to automatic conversions. While C++ rules
> >> prevent silly mistakes like
> >>
> >> typedef enum { Good, Bad } Result;
> >>
> >> Result oops() { return -1; }
> >>
> >> C unfortunately does not. So from a type safety perspective, enums are
> >> no better than #defines.

> >
> > From what and to what are enumerations in C converted?

>
> Anything that can be converted to or from an int, which includes other
> enums.
>
> --
> Ian Collins


AFAIK enumeration constants are of type int, so they are not converted to int. Example above is closely equivalent (but not 100%) to:

#define Good 0
#define Bad 1
typedef ???? Result
.....
Result oops() { return -1; }
where ???? is for char, int or unsigned int
It is obvious that -1 may or may be not within intended return range. However, nothing stops from:
int oops(); // should return only withing [100; 1000]
....
if (some error)
return -1; // forgot about spec
....

Theoretically problem may occur when ???? is char and oops() returns value out of char range or ???? is unsigned int an negative value is returned. Inboth cases compiler would issue warning about implicit conversions.

IMHO the problem is not that C enum is "type unsafe" but is in applying identical assumptions to different things.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-01-2012
Maxim Fomin <(E-Mail Removed)> writes:

> воскресенье, 1 июля 2012*г., 11:05:22 UTC+4 пользователь Ian Collins написал:
>> On 07/ 1/12 05:39 PM, Maxim Fomin wrote:
>> > воскресенье, 1 июля 2012 г., 1:34:07 UTC+4 пользователь Ian Collins написал:
>> >> On 07/ 1/12 08:57 AM, rammy wrote:
>> >>
>> >> <snip>
>> >>
>> >>> I'm living in C++ world, so I want to try my best to make the API type
>> >>> safe. Is there any way except enum could give a type safe error code
>> >>> solution ?
>> >>
>> >> You will have to accept that from a C++ programmer's perspective, enums
>> >> in C are somewhat broken due to automatic conversions. While C++ rules
>> >> prevent silly mistakes like
>> >>
>> >> typedef enum { Good, Bad } Result;
>> >>
>> >> Result oops() { return -1; }
>> >>
>> >> C unfortunately does not. So from a type safety perspective, enums are
>> >> no better than #defines.
>> >
>> > From what and to what are enumerations in C converted?

>>
>> Anything that can be converted to or from an int, which includes other
>> enums.

<snip sig>

> AFAIK enumeration constants are of type int, so they are not converted
> to int.


Agreed, but your question was about enumerations and not just their
constants so you need to consider what happens to the values of enum
objects as well as to enum constants.

> Example above is closely equivalent (but not 100%) to:
>
> #define Good 0
> #define Bad 1
> typedef ???? Result
> ....
> Result oops() { return -1; }
> where ???? is for char, int or unsigned int
> It is obvious that -1 may or may be not within intended return range.


Absolutely (though the type ???? could be any signed or unsigned integer
type). It is even possible to get an integer overflow: had the return
been "return 999;" on a system where the compiler chose an 8-bit signed
char for the enum.

> However, nothing stops from:
> int oops(); // should return only withing [100; 1000]
> ...
> if (some error)
> return -1; // forgot about spec
> ...


Showing something else that C can't express is not a very strong
argument for C's enums being how they are! But maybe you were just
saying that the enum behaviour fits with the fact that C does not have
range types.

> Theoretically problem may occur when ???? is char and oops() returns
> value out of char range or ???? is unsigned int an negative value is
> returned. In both cases compiler would issue warning about implicit
> conversions.


Well it might, but it doesn't have to. I don't think gcc does, though
there may be an option I'm missing that makes it warn about such things.

> IMHO the problem is not that C enum is "type unsafe" but is in
> applying identical assumptions to different things.


I don't know what you mean here. C's enums present the programmer with
a number of things to wary of; one of the biggest being that an enum
type is simply a new type compatible with some unknown integer type.
That's not always what people expect so, like so much of C, you really
have to know it not to get tripped up.

--
Ben.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      07-02-2012
Ben Bacarisse <(E-Mail Removed)> writes:

> Maxim Fomin <(E-Mail Removed)> writes:
> [snip]
>
>> Example above is closely equivalent (but not 100%) to:
>>
>> #define Good 0
>> #define Bad 1
>> typedef ???? Result
>> ....
>> Result oops() { return -1; }
>> where ???? is for char, int or unsigned int
>> It is obvious that -1 may or may be not within intended return range.

>
> Absolutely (though the type ???? could be any signed or unsigned integer
> type). It is even possible to get an integer overflow: had the return
> been "return 999;" on a system where the compiler chose an 8-bit signed
> char for the enum. [snip unrelated]


Strictly speaking that isn't an overflow, but an
(integer) out-of-range conversion.
 
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
Re What's The Best Practice Defining Error Codes in C Sergi Pasoev C Programming 0 06-29-2012 09:15 PM
What's The Best Practice Defining Error Codes in C ׿ǿ Zhuo, Qiang C Programming 2 11-17-2008 04:16 PM
Virtual Key Codes, Scan Codes and ASCII Codes in C gj_williams2000@yahoo.co.uk C Programming 2 08-20-2005 11:04 AM
Remember when your piano teacher taught you, "Practice, practice,practice ...?" Wayne Wastier Windows 64bit 3 06-10-2005 08:29 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments