473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Catching the unsigned int

Here's the problem: I have a macro which is called all over a large
program, and which will produce incorrect results if the argument is
an unsigned [long] int.

(It didn't originally, but an external header file changed).

What I was trying to do was catch this, by (for test purposes)
changing the macro and forcing a compiler error or warning if used
with an unsigned long int, rather than long int. This is proving more
challenging than I expected.

I tried

#undef THEMACRO
#define THEMACRO(x) CallProc(&x)
long int CallProc ( long int *x) ;

(Which gives a few errors on things that aren't lvalues but I can live
with that).

But it seems that (in this environment) I get no error if THEMACRO is
used for an unsigned int.

The code can be compiled in Microsoft Visual C++ or Metrowerks
CodeWarrior; as C or C++ language. (Setting up to compile with another
compiler would probably take more manually checking all references,
which this lazy programmer is trying to avoid).

(Background: the macro used to do x<<16 but now does
x < -32767 ? BAD_RESULT : x << 16.
So far as I can tell the effect of feeding this an unsigned int is
that the -32767 is treated as unsigned before doing the comparison.)

Thanks in advance,
----------------------------------------
Aandi Inston qu***@dial.pipex.com http://www.quite.com
Please support usenet! Post replies and follow-ups, don't e-mail them.

Nov 15 '05 #1
3 1372
Aandi Inston wrote:
Here's the problem: I have a macro which is called all over a large
program, and which will produce incorrect results if the argument is
an unsigned [long] int.

(It didn't originally, but an external header file changed).

What I was trying to do was catch this, by (for test purposes)
changing the macro and forcing a compiler error or warning if used
with an unsigned long int, rather than long int. This is proving
more challenging than I expected.

I tried

#undef THEMACRO
#define THEMACRO(x) CallProc(&x)
long int CallProc ( long int *x) ;
If you invoke THEMACRO(x) on an x which is not a long int lvalue
you violate a constraint. So the solution is for you to crank up
your error/warning levels.
(Which gives a few errors on things that aren't lvalues but I can
live with that).
Given the apparent purpose of the macro, why are you (presumably)
passing rvalues to it?
But it seems that (in this environment) I get no error if THEMACRO
is used for an unsigned int.

The code can be compiled in Microsoft Visual C++ or Metrowerks
CodeWarrior; as C or C++ language. (Setting up to compile with
another compiler would probably take more manually checking all
references, which this lazy programmer is trying to avoid).

(Background: the macro used to do x<<16 but now does
x < -32767 ? BAD_RESULT : x << 16.
So there is no function CallProc? If that's so, then you should not
have posting your earlier version. How about you show us the _real_
code you're using, rather than something which is almost but not
quite totally unlike the real code.

Note that left shifting signed integers is fraught with undefined
behaviour.
So far as I can tell the effect of feeding this an unsigned int is
that the -32767 is treated as unsigned before doing the comparison.)


Yes, that's because the arithmetic promotions require that an operation
between an unsigned int and an int invoke a promotion of the int to
unsigned int.

Show us the macro you actually have, and how you intend to use it.
Show us the invocation of the macro which is causing problems.
In other words, give us code, not descriptions.

--
Peter

Nov 15 '05 #2
Thanks for the reply.

"Peter Nilsson" <ai***@acay.com.au> wrote:
#undef THEMACRO
#define THEMACRO(x) CallProc(&x)
long int CallProc ( long int *x) ;
If you invoke THEMACRO(x) on an x which is not a long int lvalue
you violate a constraint. So the solution is for you to crank up
your error/warning levels. So there is no function CallProc? If that's so, then you should not
have posting your earlier version. How about you show us the _real_
code you're using, rather than something which is almost but not
quite totally unlike the real code.
I haven't explained myself very well. I don't want a replacement macro
for changing the code. I want to temporarily change the macro to do a
one off hunt for problems.

That's why it was being called in a few cases with things that aren't
lvalues, and why this isn't a problem. That's why it isn't an issue
that CallProc doesn't exist.
Note that left shifting signed integers is fraught with undefined
behaviour.
I didn't write the original macro. So far it seems to work, but I'll
do some more analysis.
Show us the macro you actually have, and how you intend to use it.
That part isn't the problem. I want to switch to using a different
macro where the argument is unsigned, and the aim here is to find
where to do that.
In other words, give us code, not descriptions.


I'd really prefer to avoid complications that arise from that - I see
already that the paragraph of background only complicated things - and
focus on the problem hunt only.
----------------------------------------
Aandi Inston qu***@dial.pipex.com http://www.quite.com
Please support usenet! Post replies and follow-ups, don't e-mail them.

Nov 15 '05 #3
"Peter Nilsson" <ai***@acay.com.au> wrote:
If you invoke THEMACRO(x) on an x which is not a long int lvalue
you violate a constraint. So the solution is for you to crank up
your error/warning levels.


Thanks, this confirmation that it WAS a violation led me to try more
options and compilers; I finally got it to complain with CodeWarrior.

Problem solved. (After all that, there were no unsigned values beyond
the one which I had identified as causing incorrect results).
----------------------------------------
Aandi Inston qu***@dial.pipex.com http://www.quite.com
Please support usenet! Post replies and follow-ups, don't e-mail them.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fred | last post by:
Hello, I read a file from several classes and want to catch an EOF when it appears. Having caught the EOF I want to allow the final part of the program to continue rather than just exiting. ...
4
by: interpim | last post by:
ok the function is supposed to accept only non-negative integers... but it still accepts letters input through the function. Such as '23e' still passes through. I tried to use isalpha in the or...
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.