473,569 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HOWTO: Get Warning on implicit type casts that may lead to data loss

Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?

Or you suggest use some other static source codes analysis tools?

Thanks in advance!

Nov 11 '07 #1
12 3626
On 2007-11-11 15:39, el************@ gmail.com wrote:
Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?

Or you suggest use some other static source codes analysis tools?
Any good compiler should warn when there is a risk of data loss, have
you turned up the warning levels? Good flags to always include when
compiling with gcc are -Wall and -std=c++98, there are probably more but
those are the ones I can remember right now.

--
Erik Wikström
Nov 11 '07 #2
Erik Wikström wrote:
On 2007-11-11 15:39, el************@ gmail.com wrote:
>Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?
There is no such thing as an implicit type cast. It's an implicit
conversion.
>Or you suggest use some other static source codes analysis tools?

Any good compiler should warn when there is a risk of data loss, have
you turned up the warning levels? Good flags to always include when
compiling with gcc are -Wall and -std=c++98, there are probably more but
those are the ones I can remember right now.
I would suggest to also add -pedantic and -Wextra.

Nov 11 '07 #3
Thanks Erik and Rolf!

On Nov 12, 1:45 am, Rolf Magnus <ramag...@t-online.dewrote:
There is no such thing as an implicit type cast. It's an implicit
conversion.
OK, thanks!
Any good compiler should warn when there is a risk of data loss, have
you turned up the warning levels? Good flags to always include when
compiling with gcc are -Wall and -std=c++98, there are probably more but
those are the ones I can remember right now.

I would suggest to also add -pedantic and -Wextra.
I've tried "-pedantic -Wall -Wextra -std=c++98" but see no warning
about such kind of error. Just tried a little program like this:

#include <iostream>

using namespace std;
int main ()
{
long l;
int i;

cout << "input a long number: ";
cin >l;
i = l;
cout << "the int number is: " << i << endl;

return 0;
}

Nov 11 '07 #4
I've tried options "-m64 -pedantic -Wall -Wextra -std=c++98" on both
x86-64 and PPC64, neither issued a single waring on assigning a "long"
to "int."

Nov 12 '07 #5
el************@ gmail.com wrote:
I've tried options "-m64 -pedantic -Wall -Wextra -std=c++98" on both
x86-64 and PPC64, neither issued a single waring on assigning a "long"
to "int."
You are right. I could have sworn that there is a warning for that, but I
looked into the manual and didn't find any option that gets me such a
warning. You could ask in gnu.g++.help, but it seems to me there is no
warning for that.
Nov 12 '07 #6
On Nov 12, 5:53 am, Rolf Magnus <ramag...@t-online.dewrote:
elliot.li.t...@ gmail.com wrote:
I've tried options "-m64 -pedantic -Wall -Wextra -std=c++98" on both
x86-64 and PPC64, neither issued a single waring on assigning a "long"
to "int."
You are right. I could have sworn that there is a warning for that, but I
looked into the manual and didn't find any option that gets me such a
warning. You could ask in gnu.g++.help, but it seems to me there is no
warning for that.
The problem is that it would also warn on things like:

char* p ;
int ch = getchar() ;
if ( ch != EOF ) {
*p ++ = ch ;
// ...
}

or even:

short a, b, c ;
a = b + c ;

The first is an extremely common idiom in C, and is probably
used in quite a lot of C++ code as well. The second will occur
anytime arithmetic is done on short or character types, because
of integral promotion.

The utility of the warning may be less that you might think,
too. An expression like:

int a, b, c ;
a = b + c ;

is probably even more common, and has the same risks.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 12 '07 #7
On Nov 11, 5:39 pm, elliot.li.t...@ gmail.com wrote:
Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?

Or you suggest use some other static source codes analysis tools?

Thanks in advance!
Viva64 tool can find dangerous implicit and explicit type conversion.
Viva64 is the lint-like tool for searching of errors and possible
problems in C/C++ code while porting the application to the 64-bit
platform.
http://www.viva64.com/viva64.php

Nov 12 '07 #8
On Nov 12, 12:53 pm, Rolf Magnus <ramag...@t-online.dewrote:
elliot.li.t...@ gmail.com wrote:
I've tried options "-m64 -pedantic -Wall -Wextra -std=c++98" on both
x86-64 and PPC64, neither issued a single waring on assigning a "long"
to "int."

You are right. I could have sworn that there is a warning for that, but I
looked into the manual and didn't find any option that gets me such a
warning. You could ask in gnu.g++.help, but it seems to me there is no
warning for that.
Thanks, I'll ask this question over there. I've read through GCC's
man page and info docs, but found nothing regards this neither.

Nov 12 '07 #9
On Nov 12, 6:27 pm, "karpov2...@gma il.com" <karpov2...@gma il.com>
wrote:
Or you suggest use some other static source codes analysis tools?
Viva64 tool can find dangerous implicit and explicit type conversion.
Viva64 is the lint-like tool for searching of errors and possible
problems in C/C++ code while porting the application to the 64-bit
platform.http://www.viva64.com/viva64.php
Thanks Karpov! It seems a good tool. But it's hard for us to adopt non-
FLOSS software in our development cycle currently. Do you have any
other suggestions?

Nov 12 '07 #10

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

Similar topics

4
3830
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
11
1863
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
9
7747
by: Ape Ricket | last post by:
Hi. During my program's set-up phase where it reads in the arguments it was invoked with, I programmed this: if (strcmp(argv,"-G") ==0) { geom_scaling = ON; if (i < argc-1) strcpy(geom_string,argv); }
16
5818
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program compiles fine. What is the warning shown ?
29
2488
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
11
7600
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the...
1
1342
by: Josué Andrade Gomes | last post by:
Take this code: 1: int main() 2: { 3: unsigned short x = { 1, 2, 3, 4, 5 }; 4: unsigned short sum = 0; 5: 6: for (int i = 0; i < 5; ++i) { 7: sum += x; 8: } 9: }
82
4548
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib...
13
9714
by: Rex Mottram | last post by:
I'm using an API which does a lot of callbacks. In classic callback style, each routine provides a void * pointer to carry user-defined data. Sometimes, however, the user-defined pointer is not needed which causes the compiler to spit out a "helpful" warning. For example: % cat unused.c #include <stdio.h> int foo(char *str, void *data) {
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.