How do I debug a single line function in Visual Studio? - c++

Is there a way in Visual Studio to debug a function which is only one line?
Something like:
int foo(int a) { return a + 1; }
It seems when VS enters the function, none of the data has been properly initialized, and upon inspecting the variable 'a', I get garbage data. The data is usually initialized once I step to the next line, but since this is a one line function it never seems to do that, which is quite annoying (as I will need to recompile everything just to inspect the value of a).

If it is simple function, you could start debugging and press Alt+8 for assembly code debug

I don't know if you can step instruction-wise instead of linewise but why don't you just reformat it as
int foo(int a)
{
return a + 1;
}

Related

how to know which statement got executed while debugging

In my code, sometimes when multiple function calls can be made in a single line. I do not get which function is running right now.
for example-
int foo(){
if(m==0||n==0) return 0;
return std::max(foo(a,b,m-1,n),foo(a,b,m,n-1));
}
While debugging, understanding which function was called based on all parameters becomes clumsy and sometimes doesn't even work. Is there any option to see processes within a line while debugging. I use codelite IDE.
When you are having difficulty debugging code, it usually means you are doing too much in a single line. This means you should split a complex statement into multiple statements. In your case, something like this:
int foo(){
if(m==0||n==0) return 0;
auto a = foo(a,b,m-1,n);
auto b = foo(a,b,m,n-1);
return std::max(a, b);
}
Alternatives to the current answer could be:
Stepping into statements. Not likely to work if foo calls are inlined
Debugging disassembly
These alternatives may not work, but if when they work, they allow debugging unaltered code.

Is it possible to set a multiple conditions Breakpoint in Visual Studio 2013?

does anyone knows if its possible to set a multiple condition breakpoint on a specific line in Visual Studio 2013 (C++) ?
I was trying using the '&&' but it didn't worked. I also couldn't find an answer on MSDN.
the breakpoint that i wanna set is inside the WindowProc, the condition that i wanna set is - message = WM_MOUSEMOVE, WPARAM = MK_LBUTTON
thanks in advace, Igor.
WM_MOUSEMOVE is a macrodefinition and gets replaced in the source code by the compiler during compilation. It is unknown to the debugger, so you can't use it in a breakpoint condition expression; use explicit number constant instead.
BTW, are you aware you used operator '=', which is not the same as '=='...?
Using && is allowed and should work. What's more, a lot of common C++ expressions are allowed. This page lists what is and what isn't allowed.
Note that using this kind of breakpoint will considerably slow down your application. To the point where debugging is no longer feasible. This might be what has led you to believe && isn't allowed. To overcome this particular problem you might want to use a construct like this:
//untested code
#ifdef _DEBUG
if(condition a && condition b)
{
//either output something (option A)
std::cout << "condition a and b are true"
//or create a nop statement (option B)
__nop(); //and set a breakpoint
//or create a 'nop statement' with compiler warning (option C)
int breakpoint = 0;
}
#endif
This will yield much better performance.
Since this code is only compiled in when you are compiling in debug, you can leave this bit of code in (and option B would therefore be the best). If you however want to be reminded to remove the debugging clause, option C is probably the way you wanna go. As this will generate a variable breakpoint is declared but never used warning. As kindly suggested by borisbn.
If you are using this statement a lot it's probably most useful to wrap it into a precompiler macro.

add watch shows undefined identifier visual studio 2012, cpp

I encounter the strangest behavior in VS 2012 (I'm writing in cpp).
I click "add watch" on a variable and it says "identifier is undefined".
Here's an example of the code:
for (int j=0;j<32;j++)
{
unsigned char curValue=desc1.at<unsigned char>(0,j);
printf("%s\n",curValue);
}
I had to use printf to show the value of curValue. lol.
Has anyone encountered such behavior?
Edit: more strange this occur. When debugging the following code:
int b1[8];
for (int k=0;k<7;k++)
b1[k]=0;
char q=curValue;
int t=0;
while (q!=0){
b1[t++]=q%2;
q=q/2;
}
The debugger just skips the loop with b1[k]=0;
Please note curValue is undefined even inside the loop.
Thanks!
As Joachim said: curValue is defined inside the loop. If watch window in visual studio see it as undefined value then you should turn off Compiler optimization.
Compiler optimization default is /O2 optimize for speed. To turn it off :
Go to project, right click and choose properties
Configuration Properties->C/C++->Optimization
select optimization , and change it from Maximize Speed (/O2) to Disabled (/Od)
I had optimizations turned on. That messed up my debugging.
Without the printf, the first loop has no side effects and is thus likely optimized away in an optimized build. In the second example, the loop that initializes the small array to 0 is probably replaced with an initialized data section.
You should probably try to debug with an unoptimized build.
Also note that the Visual Studio debugger has pretty good visualizers for the standard containers. So if the whole point of the first loop was just to peek at the contents of desc1, you can probably just examine it directly in the debugger.
Remember that the variable curValue is only valid inside the loop, if you try to add it to the watch when you're not in the loop then the variable is not defined.
Because curValue gets out of scope outside the for loop.
Also note that you should use %c in printf to print char. %s is used for C-style strings.
printf("%c\n",curValue);
Variable curValue is only valid inside the loop. If you will try to add it to the "watch" when you aren't in the loop, then your variable will be not defined.
Note: Better you should print this:
printf("%c\n",curValue);
Instead of this:
printf("%s\n",curValue)
Restart Visual Studio. Worked for me.

Some tests to see whether the code generated for iteration using pointers and iteration using indexing is different?

Is there a way to check if the compiler generates equivalent code for iteration using pointers and iteration using indexing???
i.e. for the codes
void f1(char v[])
{
for(int i=0; v[i]!=0;i++) use(v[i]);
}
and
void f1(char v[])
{
for(char *p = v; *p!=0; p++) use(*p);
}
I use microsoft visual C++ as my compiler......
Please help.....
Put a breakpoint in the function.
Verify that you compile in Release (otherwise it will surely be different) with debug information turned on.
Run.
Open the Assembly window to see the generated assembly (usually Alt+8).
I haven't used Visual Studio in some time, but I think there should be an option to create assembler files that you could compare.
Otherwise you can have two C files, one with each version of the function, and create object files from them. Then use a disassembler to get the assembler code, and compare the two files.

w8004 compiler warning BDS6 c/c++

It is a best practise to initialise a variable at the time of declaration.
int TMyClass::GetValue()
{
int vStatus = OK;
// A function returns a value
vStatus = DoSomeThingAndReturnErrorCode();
if(!vStatus)
//Do something
else
return(vStatus);
}
In the debug mode, a statement like this int vStatus = OK; is causing no issues during DEBUG MODE build.
The same when build in RELEASE MODE, throws a warning saying:
w8004: 'vStatus' is assigned a value that is never used.
Also, i am using the same variable further down my code with in the same function,like this if(!vStatus)and also I return the value of return(vStatus);
When I looked at the web for pointers on this debug Vs Release, compilers expect you to initialise your variable at the time of declaring it.
I am using Borland developer studio 6 with windows 2003 server.
Any pointers will help me to understand this issue.
Thanks
Raj
You initialise vStatus to OK, then you immediately assign a new value.
Instead of doing that you should initalise vStatus with a value that you're going to use.
Try doing the following instead:
int TMyClass::GetValue()
{
// A function returns a value
int vStatus = DoSomeThingAndReturnErrorCode();
if(!vStatus)
//Do something
else
return(vStatus);
}
Edit: Some clarification.
Initialising a variable, only to never use that value, and then to assign another value to the variable is inefficient. In your case, where you're just using int's it's not really a problem. However, if there's a large overhead in creating / copying / assignment for your types then the overhead can be a performance drain, especially if you do it a lot.
Basically, the compiler is trying to help you out and point out areas in your program where improvements can be made to your code
If you're wondering why there's no warning in debug mode, it's because the passes that perform dataflow analysis (which is what finds the problem) are only run as part of optimization.