Not getting 64 bit specific truncation warnings.

  • saurabhkaushal
  • Born
  • Born
  • saurabhkaushal
  • Posts: 2

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

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.
  • saurabhkaushal
  • Born
  • Born
  • saurabhkaushal
  • Posts: 2

Post 3+ Months Ago

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
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 38 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
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.