C++: Why debugger is showing parameter values unchanged? - c++

Using MinGW here to compile this C++ program:
int function(int n1, int n2){
n1=10;
n2=20;
return;
}
int main()
{
int a=89;
int b=16;
function(a, b);
return 0;
}
Why is the debugger showing the parameter values as if no new value had been assigned to them?
At the debugging point I would expect n1 to be 10 and n2 to be 20. So is this a misconception I have?
Edit: Adding requested info. I used Qt Creator as an IDE (v. 2.7.2) but this is a plain C++ project (no Qt involved). Compiler is MinGW 4.8 32 bits. Debugger is GDB.

Related

c and c++ function declaration & definition : specifying data type

I am working on porting c code to c++. The below C program compiles successfully. But in c++, it throws an error. I know its really basic thing. I've more than 100 functions declared in this way. Is there any flag we can add to compile successfully instead of modifying all the functions.
OS: Windows
IDE: Visual Studio
int sum(m,n)
int m,n;
{
return m+n;
}
This is very old style C. Nobody should have been using this for the last 30 years. You can't compile this with a C++ compiler. There are no compiler flags whatsoever for this, at least in the compilers I'm aware of (clang, gcc, microsoft compilers).
You should transform all functions that have the form
int sum(m,n)
int m,n;
{
return m+n;
}
int to the form
int sum(int m, int n)
{
return m+n;
}
If you have 100 functions it should be doable.
But there may be many other problems because C++ is not an exact superset of C.
This SO article may be interesting for you: Function declaration: K&R vs ANSI

Why does this C code compile in Codeblocks, but not in Visual Studio?

When I try to compile the following C code on Visual Studio 2017 with default C/C++ settings:
#include <stdio.h>
#include <stdlib.h>
/* function declaration */
/*Line 6*/ int max(int num1, int num2, int num3);
int main() {
/* local variable definition */
int a = 100;
int b = 200;
int c = 400;
int ret;
/* calling a function to get max value */
ret = max(a, b, c);
printf("Max value is : %d\n", ret);
return 0;
}
/* function returning the max between two numbers */
/*Line 25*/ int max(int num1, int num2, int num3) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num3;
return result;
}
I get the error(s):
Expected an Identifier: Line(s) 6,25
Expected a ";": Line(s) 25
Intellisense highlights those lines and wont let me run the code. Yet in Codeblocks (Using the default GNU GCC compiler, from mingW) this EXACT code compiles just fine. What is causing this?
Multiple sources have told me that its not due to Codeblocks using GCC compiler
and Visual Studio using "cl" compiler by default.
The same sources have told me that it is also not due to the possibility of each IDE compiling the code using different C standards.
I have named the the file extension as ".c" and I get these errors
If I try to compile the code as c++(or as a ".c++" file it works, but that's not what I want.
I want C.
I would prefer to use Visual Studio over Codeblocks due to its sleek look and menu layout. I also prefer the Visual Studio debugger.
What steps can I take to successfully compile this simple code on Visual Studio 2017?
Microsoft has a notoriously defined max macro. If the macro definition is pulled into your source for whatever reason, token substitution will wreak havoc. The result of that I'd wager is what you are seeing. Mostly because by your own admission, it happens only in Visual Studio.
The solution (and test for it) is fairly simple. One of these should "fix" your code:
Rename max to something else, like my_max.
Before including any headers, add #define NOMINMAX to suppress the definition of the macros in any included MS headers.
Beyond that you are gonna have to tinker with your project settings and see what may be improperly set. This is not a macro that should be automatically added in a simple console project.

Code Blocks incorrect c++ output

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

Why round() is known by mingw but not by visual studio compiler

Sample code which is valid and gets compiled by gcc but not by VS compiler:
#include <cmath>
int main()
{
float x = 1233.23;
x = round (x * 10) / 10;
return 0;
}
but for some reason, when I am compiling this in Visual Studio I get an error:
C3861: 'round': identifier not found
I do include even cmath as someone suggested here: http://www.daniweb.com/software-development/cpp/threads/270269/boss_loken.cpp147-error-c3861-round-identifier-not-found
Is this function in gcc only?
First of all, cmath is not guaranteed to bring round into the global namespace, so your code could fail, even with an up-to-date, standards compliant C or C++ implementation. To be sure, use std::round (or #include <math.h>.)
Note that your C++ compiler must support C++11 for std::round (<cmath>). A C compiler should support C99 for round (from <math.h>.) If your version of MSVC doesn't work after the fix I suggested, it could simply be that that particular version is pre-C++11, or is simply not standards compliant.
You also can use a boost library:
#include <boost/math/special_functions/round.hpp>
const double a = boost::math::round(3.45); // = 3.0
const int b = boost::math::iround(3.45); // = 3
Visual Studio 2010 (C99) doesn't support round but ceil and floor functions, so you may want to add your own function like;
long long int round(float a)
{
long long int result;
if (a-floor(a)>=0.5)
result = floor(a)+1;
else
result = floor(a);
return result;
};
The std::round nor #include <math.h> supported at the VS 2010 according to my experience.
I would use floor twice to get the correct rounded value,
long long int round(float a)
{
long long int result = (long long int)floor(a);
return result + (long long int)floor((a-result)*2)
};

Netbeans C++ return type of templated functions

For some reason the code completion in netbeans can't figure out the return type of templated functions. Take the following example...
struct Test
{
int val;
};
int main()
{
vector<Test> v;
Test t = {10};
v.push_back(t);
cout << v[0].val; //Netbeans gives the warning "Unable to resolve identifier val"
return 0;
}
The code compiles and runs fine but what is annoying is that I get this error all over my code when I use vectors. Also the code completion does not seem to work. When I type v[0]. there is no drop down giving me to option to choose val.
I am using netbeans 7.4 along with 64bit MinGW.
Well there seems to bug in the Netbeans 7.2 version and later it was fixed.
https://netbeans.org/bugzilla/show_bug.cgi?id=172227
You can find the complete discussion and possible resolution on the same problem from the following link. Here you can find how to resolve this problem(possibly).
Netbeans 7.2 shows "Unable to resolve identifier" , although build is successful
Follow some simple steps to get your identifiers resolved, given on the following link
Netbeans 7.2 shows "Unable to resolve identifier" , although build is successful1
try changing
struct Test
{
int val;
};
with
typedef struct
{
int val;
} Test;
in pure C "Test" would not be a defined type but "struct Test" would be. By changing to a typedef then you then have "Test" as a defined type.