Re: Res: [c-prog] Re: integer promotions

2008-11-28 Thread David Hamill
 It's never a good idea to be running as root...

 

 To unsubscribe, send a blank message to 
 mailto:[EMAIL PROTECTED].Yahoo! Groups 
 Links



 



Re: Res: [c-prog] Re: integer promotions

2008-11-27 Thread Pedro Izecksohn
--- peternilsson42 wrote:

   -Wconversion Warn for implicit conversions that may alter
a value. ... 
 
 Integral promotions don't alter the value.
 
 Maybe you need -Wsign-conversion

gcc -Wall -Wsign-conversion problem.c -o problem
cc1: error: unrecognized command line option -Wsign-conversion


Re: Res: [c-prog] Re: integer promotions

2008-11-27 Thread Pedro Izecksohn
--- Thomas Hruska wrote:


 Try compiling your code as C++ and see if there 
 is a difference.  C++ compilers tend to generate a lot more warnings as 
 the language is, generally-speaking, more strict.

[EMAIL PROTECTED] ~/programming/c++/problem
$ ls -la
total 10
drwxr-xr-x+  2 root None 4096 Nov 27 22:47 .
drwxr-xr-x+ 11 root None 4096 Nov 27 22:35 ..
-rw-r--r--   1 root None   69 Nov 27 22:37 Makefile
-rw-r--r--   1 root None  261 Nov 27 22:46 problem.cpp

[EMAIL PROTECTED] ~/programming/c++/problem
$ cat problem.cpp
#include climits
#include iostream

using namespace std;

int main (void) {
unsigned short int a;
unsigned long long int b, c;
a = USHRT_MAX;
b = (a*a);
c = ((unsigned int)a*(unsigned int)a);
cout  Why   hex  b   !=   c   ?\n;
return 0;
}

[EMAIL PROTECTED] ~/programming/c++/problem
$ cat Makefile
problem : problem.cpp
g++ -Wall -Wconversion problem.cpp -o problem

[EMAIL PROTECTED] ~/programming/c++/problem
$ make
g++ -Wall -Wconversion problem.cpp -o problem

[EMAIL PROTECTED] ~/programming/c++/problem
$ ./problem.exe
Why fffe0001 != fffe0001 ?


Re: Res: [c-prog] Re: integer promotions

2008-11-27 Thread andrew clarke
On Thu 2008-11-27 16:50:55 UTC-0800, Pedro Izecksohn ([EMAIL PROTECTED]) wrote:

 [EMAIL PROTECTED] ~/programming/c++/problem
 $ ls -la
 total 10
 drwxr-xr-x+  2 root None 4096 Nov 27 22:47 .
 drwxr-xr-x+ 11 root None 4096 Nov 27 22:35 ..
 -rw-r--r--   1 root None   69 Nov 27 22:37 Makefile

It's never a good idea to be running as root...


Re: Res: [c-prog] Re: integer promotions

2008-11-25 Thread Thomas Hruska
peternilsson42 wrote:
 Pedro Izecksohn [EMAIL PROTECTED] wrote:
 peternilsson42 wrote:
 
 [   unsigned short int a;
 a = USHRT_MAX; /* previously a = -1; */]
 
 So you made absolutely _no_ change to the semantics of
 that assignment!
   I fixed the signal to make others happy.
 
 You must be referring to Thomas' comment:
 
   BTW, you should have your compiler warnings turned up
   so that you get a warning for assigning a signed value
   to an unsigned variable.
 
 Admittedly assigning -1 to an unsigned type may look
 unintuitive, but it's actually the best way to get the
 max value without using (the ugly) U_MAX macros.
 
 So, I disagree with Thomas if he is saying that a = -1;
 is undesirable. Although I'm all for warnings about
 comparison of signed and unsigned types. [e.g. -1  1u,
 which is actually false.]

I was actually thinking more along the lines of a forced typecast once 
the warnings were turned up high enough:

a = (unsigned short)-1;

There would also have been warnings on the next line of code with the 
compiler complaining about a signed to unsigned conversion or 
something like that.  That would have been the more useful clue to the 
OP that a weird conversion was happening behind the scenes.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/



Re: Res: [c-prog] Re: integer promotions

2008-11-25 Thread Pedro Izecksohn
--- Thomas Hruska wrote:

 There would also have been warnings on the next line of code with the 
 compiler complaining about a signed to unsigned conversion or 
 something like that.  That would have been the more useful clue to the 
 OP that a weird conversion was happening behind the scenes.

  Yeah, gcc -Wconversion does not warn about it.

  Nice comment.

  May I report this to the gcc maintainers?


Re: Res: [c-prog] Re: integer promotions

2008-11-25 Thread Thomas Hruska
Pedro Izecksohn wrote:
 --- Thomas Hruska wrote:
 
 There would also have been warnings on the next line of code with the 
 compiler complaining about a signed to unsigned conversion or 
 something like that.  That would have been the more useful clue to the 
 OP that a weird conversion was happening behind the scenes.
 
   Yeah, gcc -Wconversion does not warn about it.
 
   Nice comment.
 
   May I report this to the gcc maintainers?

gcc is a very stable compiler and has been around for a really long 
time.  I'm sure there is a way to get the warning level high enough to 
warn about sign conversions.  You could ask them, but try to not present 
it as a bug - the compiler is the way it is for quite a few historical 
reasons.

One more thing to try:  Try compiling your code as C++ and see if there 
is a difference.  C++ compilers tend to generate a lot more warnings as 
the language is, generally-speaking, more strict.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/