0

This is a simple program which i wrote it with vim editor:

#include <iostream>
using namespace std;

int main()
{
    int a;
    int b, c ;
    a=(b+c+11)/3;
    cout << "x=" << a;
    cout << "\n";
        return 0;
}

We can see the warnings in visual studio in windows:

...error(s), 2 warning(s)
...\test1.cpp(7) : warning c4700: local variable 'b' used without having been initialized 
...\test1.cpp(7) : warning c4700: local variable 'c' used without having been initialized 

But, When we use gnome-terminal, we can't see warnings:

SSS@SSS:~/.cpp$ g++ test1.cpp -o test1
SSS@SSS:~/.cpp$ chmod +x test1
SSS@SSS:~/.cpp$ ./test1
x=10925
SSS@SSS:~/.cpp$

In terminal we just can see errors...
How to see these warnings?
Any command?to see warnings?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483

1 Answers1

5

Visual studio default warnings level is different from g++ default warning level.

You need to enable the warnings (I suggest -Wall) to see them.

g++ -Wall test1.cpp -o test1

prints:

test1.cpp: In function 'int main()':
test1.cpp:8:9: warning: 'b' is used uninitialized in this function [-Wuninitialized]
     a=(b+c+11)/3;
        ~^~
test1.cpp:8:9: warning: 'c' is used uninitialized in this function [-Wuninitialized]

as the message suggests -Wuninitialized is enough for this kind of warnings, but I suggest you use -Wall for starters, and turn off the warnings that you don't need if you really need that on some legacy code, the best way being to enable extra warnings, and turn the warnings into errors so people have to fix them:

g++ -Wall -Wextra -Werror ...

Also note that you cannot rely on this warning to detect all uninitialized variables. There are complex cases where the compiler cannot decide if it's initialized (see why am I not getting an "used uninitialized" warning from gcc in this trivial example?). For that you need a more specialized tool like Valgrind.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I suggest `g++ -Wall -Wextra -g test1.cpp -o test1` to get even more warnings and an executable with debug info (in DWARF format) – Basile Starynkevitch Mar 09 '18 at 12:13
  • Adding debug or not is hardly relevant to the current question (as relevant as optimization level for instance, which can affect the warnings...). I agree with `Wextra`, though. edited. – Jean-François Fabre Mar 09 '18 at 12:20
  • The newbie would want to debug his program – Basile Starynkevitch Mar 09 '18 at 12:27
  • I thought that newbies _never_ debugged their programs that's why they end up posting here :). While I agree, this is a question about warnings, not "which are the best compilation options". It's probably covered in some other thread. – Jean-François Fabre Mar 09 '18 at 12:29
  • @Jean-François Fabre maybe u r right... i agree i'm newbie in Cpp:) ... i like python... but i must learn C/C++ for college!... and i just start it right now:) 1 hour ago! anyway thank you –  Mar 09 '18 at 12:56
  • good luck, and remember: python is excellent but isn't fast enough for some applications, and you sometimes have to reuse some C or C++ codes, so better get knowledge of all of them. Then use python when needed (for instance to run C programs in loops & exploit their results) – Jean-François Fabre Mar 09 '18 at 13:01
  • and yes, learn to use a debugger right away. Visual C++ debugger is system friendly enough. gdb needs a good interface or it's just horrible to work with. – Jean-François Fabre Mar 09 '18 at 13:02