Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > I'm a newbie. Is this code ugly?

Reply
Thread Tools

I'm a newbie. Is this code ugly?

 
 
Christopher Dearlove
Guest
Posts: n/a
 
      01-27-2010
"Richard Herring" <junk@[127.0.0.1]> wrote in message
news:(E-Mail Removed)...
> So you're using wrapping as a poor substitute for checking that t is in
> range. On most systems, t will reach a value which exceeds the available
> memory long before it wraps to a negative value.


And, while it usually happens to work, wrapping of signed integers
is actually undefined behaviour.

If you are using unsigned integers (like size_t) it's easy to check wrapping
of addition (although as noted that probably is the wrong test). If a, b, c
are such values and

c = a + b;

Then wrapping has ocurred if (and only if) c < a (or, equally well, c < b).

What I don't know how to do without using division - any solutions welcome
- is to check the overflow of a * b (unsigned again). c = a * b can overflow
but with c > a and c > b. That's not the issue here, but it's one I have
wanted
(for smaller unsigned types than size_t).

Of course with regard to the original code this is nit-picking fine points
of code that the only thing to actually do with it is bin it and start
again.
It's instructive that the poster has had to revise the code at least twice
since posting to fix bugs. The polite term is unmaintainable.


 
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 is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM
Re: Code Behind vs. no code behind: error Ben Miller [msft] ASP .Net 1 06-28-2003 01:46 AM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ian ASP .Net 0 06-25-2003 01:14 PM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ron ASP .Net 1 06-24-2003 07:18 PM



Advertisments