1

I compile this code with g++ -Wall, get no warnings/errors:

#include "stdio.h"

int main() {
        long x = 1000000000000;
        int y = x;
        printf("%ld %d\n", x, y);
        return 0;
}

The output is something one would expect:

1000000000000 -727379968

But shouldn't the compiler prevent from implicit conversion/truncation in this case?

g++ (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
  • 1
    *Prevent* it ? How about *warn* you about it? (You do have warnings turned on, right? It says you do, but none are emitted? not even "potential loss of data" type warnings?) – WhozCraig Aug 30 '13 at 16:39
  • That's correct. I tried using -Wconversion I just found here: http://stackoverflow.com/questions/5263267/why-does-c-allows-implicit-conversion-from-int-to-unsigned-int, but the code still compiles silently. – AlphaStream Aug 30 '13 at 16:42
  • How odd. Now I have to try this on my clang. Your gcc is older than dirt, but I'm somewhat taken-back something like this isn't caught. Edit: clang catch this immediately "Implicit conversion loses integer precision". just fyi. – WhozCraig Aug 30 '13 at 16:44
  • `-Wall` doesn't cover all warnings. If you want more warnings, you could use clang and the flag `-weverything` – olevegard Aug 30 '13 at 16:44
  • And - yes, I was expecting g++ to prevent me from potential data loss. Just the way it wouldn't allow to assign a double to an integer. – AlphaStream Aug 30 '13 at 16:45
  • Both of those (assigning a double to an int and this) aren't compile-time killers (unless you're compiling with -Werror), but you should at least be getting a conversion warning(s) (save for the case where `long` and `int` are the same size on your platform, which the standard supports) – WhozCraig Aug 30 '13 at 16:48
  • However, assigning double _does_ prevent the code from compiling. I also tried using long long instead of long - still no warnings. – AlphaStream Aug 30 '13 at 16:53
  • Well, you're not hallucinating. I found a gcc 4.2 on my Darwin rig and it exhibits the same symptoms regarding `-Wall -Wconversion`. However neither it nor clang abort the compile on a `double` assigned to an `int`, even with both. This may seem odd, but did you try `Wshorten-64-to-32` (its pretty obvious what it does, but it may not be in 4.1). That *does* issue a warning on my 4.2 kit: "warning: implicit conversion shortens 64-bit value into a 32-bit value" – WhozCraig Aug 30 '13 at 16:57
  • I got warning with gcc4.7.1/clang3.3 once x is const or constexpr. – Jarod42 Aug 30 '13 at 17:01
  • Just tried Wshorten-64-to-32, got 'unrecognized command line option'. Probably, it appeared in 4.2? And thanks for doing this research! – AlphaStream Aug 30 '13 at 17:07

1 Answers1

8

I believe you're looking for -Wconversion which is NOT included in the -Wall. I definitely got the warning in g++ 4.4 and 4.5 but I don't have access to test earlier versions. warning: conversion to 'int' from 'long long int' may alter its value (I used long long because I was generating a 32 bit build where long is still 32 bits).

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 1
    +1 That would explain it. I never knew that wasn't covered in -Wall. Good to know, Mark. Thanks! – WhozCraig Aug 30 '13 at 16:43
  • Still no luck: `$ g++ overflow.cpp -Wall -Wconversion && ./a.out 1000000000000 -727379968` – AlphaStream Aug 30 '13 at 16:48
  • Or, so it really seems to have to do with my older-then-dirt compiler like WhozCraig suggested earlier. Thanks Mark B! – AlphaStream Aug 30 '13 at 16:57
  • 1
    Just for the sake of completeness: it appears that `-Wconversion` is not included either in `-Wextra` nor in `-pedantic` (GCC 4.7). I have to admit I'm kinda baffled by this, here's one more flag I'll have to add to my usual ones. +1 – syam Aug 30 '13 at 17:08