Not getting 64 bit specific truncation warnings.

  • saurabhkaushal
  • Born
  • Born
  • No Avatar
  • Joined: Feb 21, 2009
  • Posts: 2
  • Status: Offline

Post February 21st, 2009, 1:12 am

Dear All,

I am porting my C/C++ project (currently functioning on 32 bit machine) into 64 bit SUSUE linux machine
uname -a
Linux msglxd03 2.6.5-7.97-smp #1 SMP Fri Jul 2 14:21:59 UTC 2004 x86_64 x86_64 x86_64 GNU/Linux

I am trying to get the warning Specific to long to int and pointer to int assignments.

===================================
Written warnings2.c file which conatin :

Code: [ Select ]
#include<stdio.h>
#include<stdlib.h>
int main()
{
unsigned int i;
unsigned long z = -1L;

printf("size of unsigned is %d bytes!\n", sizeof(z));
printf("size of int is %d bytes!\n", sizeof(i));

i = z;
printf("\ni is = %d",i);
i = &z;
printf("\ni is = %d",i);
return 1;
}
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int main()
  4. {
  5. unsigned int i;
  6. unsigned long z = -1L;
  7. printf("size of unsigned is %d bytes!\n", sizeof(z));
  8. printf("size of int is %d bytes!\n", sizeof(i));
  9. i = z;
  10. printf("\ni is = %d",i);
  11. i = &z;
  12. printf("\ni is = %d",i);
  13. return 1;
  14. }

==========================
After compiling (either with gcc or cc)

Code: [ Select ]
$ gcc -m64 -Wall -march=x86-64 -o war2.o warnings2.c


it gave me following warning:

Quote:
warnings2.c: In function main:
warnings2.c:9: warning: format %d expects type int, but argument 2 has type long unsigned int
warnings2.c:10: warning: format %d expects type int, but argument 2 has type long unsigned int
warnings2.c:14: warning: assignment makes integer from pointer without a cast


There isn't any warning for long to int assignment(truncation) @ line 12.

Quote:
On sparcv9 it is giving warnings when used >> CC +w2 -xarch=v9
"warnings2.cpp", line 12: Warning: Conversion of 64 bit type value to "int" causes truncation.
1 Warning(s) detected.


Could anybody please tell me how could we get the similar warnings on Linux?
Which all options are need with gcc to get the truncation warnings on 64-bit linux machine?
We have already tried -m64 -Wall -mcpu=x86-64 -Wconversion -pedantic and -Wshorten-64-to-32, but didn't get truncation warnings ?

Thanks in advance,

Regards.
Moderator Remark: added code tags
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 21st, 2009, 1:12 am

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post February 22nd, 2009, 11:26 pm

Not sure about the 64-bit truncation warnings you're expecting, but the reason you're getting warnings about your format strings is because you're not using the correct type modifiers. You should be using %lu for unsigned longs and %u for regular unsigned ints.

As for this:
Code: [ Select ]
i = &z;

That simply isn't valid. You're trying to assign a pointer to an integer variable.
The Beer Monocle. Classy.
  • saurabhkaushal
  • Born
  • Born
  • No Avatar
  • Joined: Feb 21, 2009
  • Posts: 2
  • Status: Offline

Post February 23rd, 2009, 2:11 am

yes I accept your points but idea is to concentrate abt the warnings that should be flased when you assign long to int, as on 64 bit Linux machine long is 64 bit while int is 32 bit so such as assignment causes truncation. My question was, why the warning is not coming while same code is giving truncation warnings on Solaris 64 bit machine.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 23rd, 2009, 3:22 am

http://gcc.gnu.org/ml/gcc-help/2008-08/msg00024.html

That makes me think you either need -Wconversion and/or a more recent version of gcc.

// Edit
Yep.

Code: [ Select ]
joebert@frankenstien:~/Desktop/test$ gcc -m64 -Wall -Wconversion -march=x86-64 -o war2.o warnings2.c
warnings2.c: In function ‘main’:
warnings2.c:6: warning: negative integer implicitly converted to unsigned type
warnings2.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
warnings2.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
warnings2.c:11: warning: conversion to ‘unsigned int’ from ‘long unsigned int’ may alter its value
warnings2.c:13: warning: assignment makes integer from pointer without a cast
  1. joebert@frankenstien:~/Desktop/test$ gcc -m64 -Wall -Wconversion -march=x86-64 -o war2.o warnings2.c
  2. warnings2.c: In function ‘main’:
  3. warnings2.c:6: warning: negative integer implicitly converted to unsigned type
  4. warnings2.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
  5. warnings2.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
  6. warnings2.c:11: warning: conversion to ‘unsigned int’ from ‘long unsigned int’ may alter its value
  7. warnings2.c:13: warning: assignment makes integer from pointer without a cast
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 55 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.