Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > unsigned char and -1

Reply
Thread Tools

unsigned char and -1

 
 
Alex Vinokur
Guest
Posts: n/a
 
      10-25-2005
I came across the following piece of code:

#define ERROR -1
#define STATUS0 0
#define STATUS1 1
#define STATUS2 2

unsigned char foo()
{
if (/* condition-A */) return ERROR; /* is that safe? */
if (/* condition-B */) return STATUS0;
if (/* condition-C */) return STATUS1;
return STATUS2;
}

Is '-1 returned as unsigned char' safe in the given context?


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      10-25-2005
Alex Vinokur wrote:
> I came across the following piece of code:
>
> #define ERROR -1
> #define STATUS0 0
> #define STATUS1 1
> #define STATUS2 2
>
> unsigned char foo()
> {
> if (/* condition-A */) return ERROR; /* is that safe? */
> if (/* condition-B */) return STATUS0;
> if (/* condition-C */) return STATUS1;
> return STATUS2;
> }
>
> Is '-1 returned as unsigned char' safe in the given context?


In the given (restricted) context, yes, it returns UCHAR_MAX.
[Any value not in the range of an unsigned type is converted
modulo 1+UTYPE_MAX.]

However, in broader contexts, it's not so safe, e.g. ...

if (foo() == ERROR)

This condition will likely fail even though foo returns ERROR.
This is because the UCHAR_MAX will likely be promoted to a
int, and -1 is not UCHAR_MAX.

--
Peter

 
Reply With Quote
 
 
 
 
Alex Vinokur
Guest
Posts: n/a
 
      10-25-2005

"Peter Nilsson" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) oups.com...
> Alex Vinokur wrote:
> > I came across the following piece of code:
> >
> > #define ERROR -1
> > #define STATUS0 0
> > #define STATUS1 1
> > #define STATUS2 2
> >
> > unsigned char foo()
> > {
> > if (/* condition-A */) return ERROR; /* is that safe? */
> > if (/* condition-B */) return STATUS0;
> > if (/* condition-C */) return STATUS1;
> > return STATUS2;
> > }
> >
> > Is '-1 returned as unsigned char' safe in the given context?

>
> In the given (restricted) context, yes, it returns UCHAR_MAX.
> [Any value not in the range of an unsigned type is converted
> modulo 1+UTYPE_MAX.]
>
> However, in broader contexts, it's not so safe, e.g. ...
>
> if (foo() == ERROR)


And in this context?

unsigned char ch1, ch2;
ch1 = foo();
/* Stuff-1 */
if (ch1 == ch2)
{
/* Stuff-2 */
}

>
> This condition will likely fail even though foo returns ERROR.
> This is because the UCHAR_MAX will likely be promoted to a
> int, and -1 is not UCHAR_MAX.

[snip]


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




 
Reply With Quote
 
makc.the.great@gmail.com
Guest
Posts: n/a
 
      10-25-2005

Alex Vinokur wrote:
> And in this context?
>
> unsigned char ch1, ch2;
> ch1 = foo();
> /* Stuff-1 */
> if (ch1 == ch2)
> {
> /* Stuff-2 */
> }


and in this context ch2 is undefined :-p

 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      10-25-2005

<(E-Mail Removed)> wrote in message news:(E-Mail Removed) ups.com...
>
> Alex Vinokur wrote:
> > And in this context?
> >
> > unsigned char ch1, ch2;
> > ch1 = foo();
> > /* Stuff-1 */
> > if (ch1 == ch2)
> > {
> > /* Stuff-2 */
> > }

>
> and in this context ch2 is undefined :-p
>


Stuff-1 can change ch2.


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-25-2005
Alex Vinokur wrote:
> I came across the following piece of code:
>
> #define ERROR -1
> #define STATUS0 0
> #define STATUS1 1
> #define STATUS2 2
>
> unsigned char foo()
> {
> if (/* condition-A */) return ERROR; /* is that safe? */

[...]
> Is '-1 returned as unsigned char' safe in the given context?


[comp.lang.c answer]
Yes, it is safe in the sense that UCHAR_MAX will be returned.
No, it is not safe, because 'ERROR' invades the namespace reserved to
the implementation.

As a general rule, crossposting to comp.lang.c and comp.lang.c++ is a
poor idea. Not only are these different languages, but when the syntax
is the same in each language, the semantics may differ, and when the
syntax and semantics agree, the accepted best practice may differ.
 
Reply With Quote
 
makc.the.great@gmail.com
Guest
Posts: n/a
 
      10-25-2005

Alex Vinokur wrote:
> > > unsigned char ch1, ch2;
> > > ch1 = foo();
> > > /* Stuff-1 */
> > > if (ch1 == ch2)
> > > {
> > > /* Stuff-2 */
> > > }

> >
> > and in this context ch2 is undefined :-p
> >

>
> Stuff-1 can change ch2.


A comment can change variable? Now that's something new :-p''

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      10-26-2005
Martin Ambuhl wrote:
> Alex Vinokur wrote:
> > I came across the following piece of code:
> >
> > #define ERROR -1
> > #define STATUS0 0
> > #define STATUS1 1
> > #define STATUS2 2
> >
> > unsigned char foo()
> > {
> > if (/* condition-A */) return ERROR; /* is that safe? */

> [...]
> > Is '-1 returned as unsigned char' safe in the given context?

>
> [comp.lang.c answer]
> Yes, it is safe in the sense that UCHAR_MAX will be returned.
> No, it is not safe, because 'ERROR' invades the namespace reserved
> to the implementation.


True, although EXXXX identifiers are only reserved if the code
includes <errno.h>. That said, it is best to avoid defining such
identifiers at all, in case the code ever gets included by other
code that might use <errno.h>.

--
Peter

 
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
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Padding bits and char, unsigned char, signed char Ioannis Vranos C Programming 6 03-29-2008 10:55 AM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments