Is this risky? 
Author Message
 Is this risky?

Hi,

Given:

typedef unsigned char byte;
#define MAXSIZE 256
byte data[MAXSIZE];
byte checksum = 0, i;

and a for loop of
for (i = 0; i < MAXSIZE; i++)
{
        checksum +=data[i];
        /* other stuff */

Quote:
}

Is this risky or undefined behaviour?. Would I be better using something
like:-

unsigned int checksum = 0;
and in for loop do:-
checksum = (checksum + data[i])  % MAXSIZE?

Thanks, matthew.



Sun, 03 Apr 2005 07:07:04 GMT  
 Is this risky?

Quote:
> Hi,

> Given:

> typedef unsigned char byte;
> #define MAXSIZE 256
> byte data[MAXSIZE];
> byte checksum = 0, i;

> and a for loop of
> for (i = 0; i < MAXSIZE; i++)
> {
>         checksum +=data[i];
>         /* other stuff */
> }

> Is this risky or undefined behaviour?. Would I be better using something
> like:-

> unsigned int checksum = 0;
> and in for loop do:-
> checksum = (checksum + data[i])  % MAXSIZE?

> Thanks, matthew.

AFAICS it's not 'undefined behaviour', though it is 'implementation
defined' behaviour since it depends on what an unsigned char means to the
implementation. For example, I'm currently writing software on a machine
where an 'unsigned char' would have a range from 0 to (2**32)-1.

Assuming that you want an 8 bit wide checksum, that the data you're
producing a checksum of is limitted to 8 bits wide also then the code you
present at first isn't portable: it assumes that unsigned chars are of the
range 0-255 which may not be the case.

Your second example is closer to the mark, but I don't think you should use
'MAXSIZE'. The width of your checksum is independant from the length of the
checksummed data, so 'MAXSIZE' shouldn't be used in that context (say, you
want an 8 bit checksum of 128 byte packets instead of 256 byte packets but
you still want a checksum in the range 0-255).

Ian Woods



Sun, 03 Apr 2005 08:27:39 GMT  
 Is this risky?
On Wed, 16 Oct 2002 12:07:04 +1300, Matthew

Quote:

>Hi,

>Given:

>typedef unsigned char byte;
>#define MAXSIZE 256
>byte data[MAXSIZE];
>byte checksum = 0, i;

>and a for loop of
>for (i = 0; i < MAXSIZE; i++)

If byte is an 8-bit unsigned integer type, then this loop will never
end.  When i increments past 255, it will wrap to 0.

It is an 8-bit signed integer type, the loop will invoke undefined
behavior when i increments past 127
.

Quote:
>{
>        checksum +=data[i];

If byte is signed, this will invoke undefined behavior as soon as the
sum exceeds SCHAR_MAX.  If it is unsigned, the arithmetic will be done
modulo UCHAR_MAX+1.

Quote:
>        /* other stuff */
>}

>Is this risky or undefined behaviour?. Would I be better using something
>like:-

>unsigned int checksum = 0;
>and in for loop do:-
>checksum = (checksum + data[i])  % MAXSIZE?

MAXSIZE is the size of your buffer.  What does it have to do with your
checksum?

<<Remove the del for email>>



Sun, 03 Apr 2005 12:48:50 GMT  
 Is this risky?

Quote:

> Hi,

> Given:

> typedef unsigned char byte;
> #define MAXSIZE 256
> byte data[MAXSIZE];
> byte checksum = 0, i;

> and a for loop of
> for (i = 0; i < MAXSIZE; i++)
> {
>         checksum +=data[i];
>         /* other stuff */
> }

> Is this risky or undefined behaviour?. Would I be better using
> something like:-

To start with, look at your loop variable, i.  It is typed as a
byte.  There is no guarantee that it can hold a value larger than
255.  So the exit condition may never be fulfilled, and you have
hung your machine chasing its own tail in an infinite loop.

ints are the things the machine can handle best.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Sun, 03 Apr 2005 12:51:08 GMT  
 Is this risky?

Quote:
> typedef unsigned char byte;
> #define MAXSIZE 256
> byte data[MAXSIZE];
> byte checksum = 0, i;

Fine. Just keep in mind that an unsigned char has 8 bits at minimum. Also,
it's not clear where 'data' is defined, but it could be unitialized at the
moment.

Quote:
> and a for loop of
> for (i = 0; i < MAXSIZE; i++)
> {
>         checksum +=data[i];
>         /* other stuff */
> }

> Is this risky or undefined behaviour?. Would I be better using something
> like:-

It's fine, but if you want an 8-bit checksum, better to trim the value with
an 8-bit mask, hence:

   checksum += (data[i] & 0xFF);

Quote:
> unsigned int checksum = 0;
> and in for loop do:-
> checksum = (checksum + data[i])  % MAXSIZE?

% is 'expansive'. A simple bit mask is cheaper...

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
-- Albert Camus.



Sun, 03 Apr 2005 13:41:50 GMT  
 Is this risky?

Quote:
>>typedef unsigned char byte;
>>#define MAXSIZE 256
>>byte data[MAXSIZE];
>>byte checksum = 0, i;

>>and a for loop of
>>for (i = 0; i < MAXSIZE; i++)

> If byte is an 8-bit unsigned integer type, then this loop will never
> end.  When i increments past 255, it will wrap to 0.

Oops, I missed this one.

Quote:
> It is an 8-bit signed integer type, the loop will invoke undefined
> behavior when i increments past 127
> .

The definition of 'byte' was given : 'unsigned char'

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
-- Albert Camus.



Sun, 03 Apr 2005 13:44:43 GMT  
 Is this risky?

Quote:

>> typedef unsigned char byte;
>> #define MAXSIZE 256
>> byte data[MAXSIZE];
>> byte checksum = 0, i;
>> for (i = 0; i < MAXSIZE; i++)
>> {
>>         checksum +=data[i];
>>         /* other stuff */
>> }

>> Is this risky or undefined behaviour?.
> It's fine,

It's wrong. On a 8-bit char implementation (0-255), it will loop forever.
Better to use 'unsigned' for i:

typedef unsigned char byte;
#define MAXSIZE 256
byte data[MAXSIZE];
byte checksum = 0;
unsigned i;

for (i = 0; i < MAXSIZE; i++)
{
   checksum += (data[i] & 0xFF);

Quote:
}

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
-- Albert Camus.


Sun, 03 Apr 2005 13:48:20 GMT  
 Is this risky?
On Wed, 16 Oct 2002 12:07:04 +1300, Matthew said:

Quote:
> Hi,

> Given:

> typedef unsigned char byte;
> #define MAXSIZE 256
> byte checksum = 0, i;

> and a for loop of
> for (i = 0; i < MAXSIZE; i++)

Assuming a range of 0 to 255 for unsigned char, the condition
here will always be true, and this is an infinite loop.

Quote:
> Is this risky or undefined behaviour?. Would I be better using something
> like:-

> unsigned int checksum = 0;
> and in for loop do:-
> checksum = (checksum + data[i])  % MAXSIZE?

What do you want to do? There's nothing undefined in overflowing
unsigned types, if that's your question... except that sometimes
the overflows will surprise you (as above).

Cheers,
Dave.

--
           David Neary,
     E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html



Sun, 03 Apr 2005 16:00:35 GMT  
 Is this risky?
On Wed, 16 Oct 2002 05:44:43 UTC, Emmanuel Delahaye

Quote:


> >>typedef unsigned char byte;
> >>#define MAXSIZE 256
> >>byte data[MAXSIZE];
> >>byte checksum = 0, i;

> >>and a for loop of
> >>for (i = 0; i < MAXSIZE; i++)

> > If byte is an 8-bit unsigned integer type, then this loop will never
> > end.  When i increments past 255, it will wrap to 0.

> Oops, I missed this one.

> > It is an 8-bit signed integer type, the loop will invoke undefined
> > behavior when i increments past 127
> > .

> The definition of 'byte' was given : 'unsigned char'

can you save 20000 in an unsigned char?
can you save 0x100 in an unsigned char?
can you save 256 in an unsigned char?

When you says YES then your char has definitly more than 8 bits and
-256 and +255 will be alowed for signed char. If your char has the
common wide of 8 bits you can't get 256 in.

You can test it by yourself!

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de   eComStation Reseller in Germany



Sun, 03 Apr 2005 22:59:00 GMT  
 Is this risky?

Quote:

> > > It is an 8-bit signed integer type, the loop will invoke undefined
> > > behavior when i increments past 127
> > > .

> > The definition of 'byte' was given : 'unsigned char'

> can you save 20000 in an unsigned char?
> can you save 0x100 in an unsigned char?
> can you save 256 in an unsigned char?

> When you says YES then your char has definitly more than 8 bits and
> -256 and +255 will be alowed for signed char. If your char has the
> common wide of 8 bits you can't get 256 in.

How is this relevant to the discussion?

Eric Schmidt



Mon, 04 Apr 2005 05:27:22 GMT  
 Is this risky?

Quote:


>> > > It is an 8-bit signed integer type, the loop will invoke undefined
>> > > behavior when i increments past 127
>> > > .

>> > The definition of 'byte' was given : 'unsigned char'

>> can you save 20000 in an unsigned char?
>> can you save 0x100 in an unsigned char?
>> can you save 256 in an unsigned char?

>> When you says YES then your char has definitly more than 8 bits and
>> -256 and +255 will be alowed for signed char. If your char has the
>> common wide of 8 bits you can't get 256 in.

> How is this relevant to the discussion?

#include <limits.h>

void forever(void)
{
    unsigned char i;
    for (i = 0; i < (UCHAR_MAX + 1); i++); /* for (;;); */

Quote:
}

Change "i" to something that can hold values larger than UCHAR_MAX + 1,
and the loop is no longer infinite...

See original post, where this error was made...



Tue, 05 Apr 2005 12:58:45 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. A risky way to get RC1 to optimize your code

2. I am new to programming and am lost

3. 'conservative' GC == 'risky' GC

4. how good am I? Am I Good Enough????

5. Determine where I am running

6. System.Threading.Timer , am I doing this correctly ?

7. Am i connected?

8. Request - Opinions on Book I am considering:

9. I am puzzled: __nogc new = LNK2001

10. Am I in the wrong NG???

11. I am a convert to .NET

12. Am i being studpid, Forms question

 

 
Powered by phpBB® Forum Software