0

I have the following code:

#include <iostream>

using namespace std;

Sum (int a, int b)
{
    int x = a - b;
    //cout << x << " \n";
    return x;
}

int main()
{
    int s1 = Sum(3, 6);
    cout << s1;

    return 0;
}

System info: Win 7 Sp1 x64 Ultimate/Professional or Win 8.1 x64 Code Blocks 16.01 MinGW Debugger name and version: GNU gdb (GDB) 7.6.1 compiler: GNU GCC Compiler

This code compiles with no problems, but this IS the problem, there should be errors.

1) Function Sum, has no return value, on http://cpp.sh/ it doesn't let me compile because of this.

2) Variable's s1 value is -3 whether I write "return x" or not.

It somehow passes the value of x everytime BUT if I uncomment the cout statement above the "return x" everything starts to work out as expected, what the hell :) --> s1 will have a random value when no return statement is in place (because it was not initialized prior to being used for the function call) and -3 when the return is there.

I've tried this on 3 separate computers and they all exhibit the same behaviour. So I don't think the machine is the problem. I also tried using a different compiler but I don't know if I configured them correctly and they don't have a debugger right ? I tried Borland c++ and Digital Mars. Borland has a new version 10.1 instead of the 5.5 that codeblocks supports and I couldn't make the new one work. I don't even know if this is a compiler or program issue ?

I'm trying to learn C++ and this is very annoying. Our teacher is using the same software in class but on Linux and it works perfectly.

Off topic: Is there a way to insert code with line numbers here ? First post here so i'm still new at this :).

Thank you !

Rama
  • 3,222
  • 2
  • 11
  • 26
  • 1
    Use stricter compiler options. – juanchopanza Dec 17 '16 at 12:46
  • Which version of GCC are you using? Because like you said that should not compile with any version of any C++ compiler. – Some programmer dude Dec 17 '16 at 12:51
  • What is THE question? Why an incorrect code behaves undefinedly? – Fureeish Dec 17 '16 at 12:51
  • ^^ What these guys said. Case and point: https://ideone.com/b7dFUe – StoryTeller - Unslander Monica Dec 17 '16 at 12:52
  • Wow, thanks for the fast responses. 1) How can I find out what version of GCC I'm using ? 2) the question is: Why incorrect code compiles and behaves like this and who is the culprit ? The software, the compiler etc. and what are my solutions ? Thanks – Mihai Nicolau Dec 17 '16 at 13:01
  • `g++ --version` for the version. – StoryTeller - Unslander Monica Dec 17 '16 at 13:03
  • And what stricter compiler options should I use, I'm a beginner and I'm overwhelmed by all the different options available in the compiler menu screen. Also I don't think stricter compiler options would solve the problem of values being passed around without return statement ? – Mihai Nicolau Dec 17 '16 at 13:04
  • When an incorrect code compiles it's obviously fault of a compiler. Default Code::Blocks compiler is really old. I would suggest you using a lastest one. You can read how to install one [here](http://stackoverflow.com/questions/31171979/enabling-std-c14-flag-in-codeblocks). – Fureeish Dec 17 '16 at 13:05
  • Compiler version as taken from the info on Code Blocks website: The codeblocks-16.01mingw-setup.exe file includes additionally the GCC/G++ compiler and GDB debugger from TDM-GCC (version 4.9.2, 32 bit, SJLJ) – Mihai Nicolau Dec 17 '16 at 13:10
  • 1
    To fill things in a bit: C, back in the olden days, had "implicit int": function declarations with no return type indicated that the function returned `int`. That was removed for C99, but many compilers continue to support it so the old code doesn't break. – Pete Becker Dec 17 '16 at 13:15
  • Changed compiler to what user above suggested: http://stackoverflow.com/questions/31171979/enabling-std-c14-flag-in-codeblocks And it behaves the same :( – Mihai Nicolau Dec 17 '16 at 14:08
  • `2) Variable's s1 value is -3 whether I write "return x" or not.` Well, you have a `-` sign instead of `+`. – HolyBlackCat Dec 17 '16 at 14:10

2 Answers2

1

Go to "Project" -> "Build Options" -> "Compiler Settings" tab -> "Compiler Flags"

And disable -fpermissive

-fpermissive Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive will allow some nonconforming code to compile.

Or disable it using pragma on the top of your code:

#pragma GCC diagnostic ignored "-fpermissive"

Also you could try to add the flag "-pedantic" in "Compiler Flags" tab

BTW:

If you try online:

#pragma GCC diagnostic error "-fpermissive"

using namespace std;

Sum (int a, int b)
{
    int x = a - b;
    //cout << x << " \n";
    return x;
}

int main()
{
    int s1 = Sum(3, 6);
    cout << s1;

    return 0;
}

You got exactly same behavior you described!

Rama
  • 3,222
  • 2
  • 11
  • 26
0

As Rama said, you might have enabled -fpermissive in your codeblock. Go to "Project" -> "Build Options" -> "Compiler Settings" tab -> "Other options" and delete -fpermissive.

pharask
  • 322
  • 7
  • 15