Qt Creator debugger just show local variables - c++

I am using QtCreator5.14.0 MinGW-64bit and I wrote this code :
(do not check the code, it is correct , my problem is another thing)
#include <iostream>
using namespace std;
#define MAX 100
int Solutions[MAX]={0,1};
int CountOfCalls = 0;
long int f(int a){
if(a==0)
return 0;
if(Solutions[a])
return Solutions[a];
CountOfCalls++;
return (Solutions[a] = f(a-1) + f(a-2));
}
int main(){
cout<<f(40)<<endl;
cout<<"Count of calls : "<<CountOfCalls<<endl;
return 0;
}
when I begin debugging at the first code in the main , right pane doesn't show global variables(here is Solutions array and CountOfCalls variable) just shows local variables.
debugging : https://i.imgur.com/YvVvbyk.png
my debugger settings :
General : https://i.imgur.com/Ziz8vHJ.png
GDB : https://i.imgur.com/FdjsGmf.png
GDB Extended : https://i.imgur.com/eJ8Bq4Y.png
Locals & Expressions : https://i.imgur.com/ef4KxSj.png
I can right click and choose add new expression evaluator to have the Solutions array and CountOfCalls variable in the right pane , but it is boring for big source codes to add everything you want .
how to tell debugger to evaluate all global and local variables automatically?

Related

Execute Microsoft Visual Studio project multiple times with different parameters

I would ask for your help and apologise if the question doesn't make sense.
I have a Microsoft Visual Studio project which I want to execute it multiple times in one go, and every time I will change one parameter.
Please see below the concept:
#include <iostream>
using namespace std;
int size_list = 2;
int my_list[2] = { 5, 6 };
int main()
{
for (int i = 0; i < size_list; i++)
{
cout<<"the number is "<<my_list[i]<<endl;
}
return 0;
}
So, I would like to replace the loop and instead I will have each element of my_list as parameters.
Is there any way to do so?
Thanks
You can pass the parameters in main().
int main(int argc, char* argv[]){std::cout<< argv[1];}
And use script like
for ($i=0;$i -lt 5; $i++)
{
#your CPP project and parameters array
Start-Process test.exe $arr[0]
Wait-Process test
}

How to modify value of variable automatically at runtime when hitting a breakpoint and continuing execution in VS?

We can change value manually by changing in variable tooltip or local/auto/watch window.
But I want to change value of variable automatically to some specific hardcoded value or based on a code snippet.
For eg.-
int main()
{
int a=0,b=1,c=2;
//bla bla
for(int i=0; i<100; ++i)
{
executeMe();
}
//bla bla
}
I want to put a breakpoint on line "executeMe()" and change value of 'b' to hardcoded value 3 or based on variable value 'c', so executing instruction 'b=c'. And continues execution without stopping everytime on breakpoint.
How to do this in VS?
Use the 'Print a message:' option instead of a macro. Values from code can be printed by placing them inside {}. The key is that VS will also evaluate the content as an expression - so {variable_name=0} should achieve the same as the macro example.
Thanks to Tom McKeown for this solution on stackoverflow.com/a/15415763/2328412
You could use #if preprocessor directives, which is similar with below code.
int a = 0, b = 1, c = 2;
for (int i = 0; i < 100; ++i)
{
#if DEBUG
b=3;
#endif
executeMe();
}

1D Peak, on execution says an error has caused the code to be stop working

I am trying to find the 1D peak through Divide and Conquer technique in this particular question,
my program even though it runs,
but at the time of giving the final output it says that there has been some problem with the execution,
I have got the answer from a different method, but I would like to know where am I at fault here.
#include<iostream>
using namespace std;
int a[8];
class pg1
{
public:
int func(int n)
{
if(a[n] <= a[n+1])
{
func(n++);
}
else if(a[n] <=a [n-1])
{
func(n--);
}
else
{
return n;
}
}
};
int main()
{
pg1 ob;
for(int i=0;i<8;i++)
{
cin >> a[i];
}
int x = ob.func(4);
cout << endl << x;
return 0;
}
Input-
5
6
8
5
4
3
6
4
Errors are-
1D Peak.exe has stopped working.
A problem caused the program to stop working correctly.Windows will close the program and notify you aif a solution is available.
End Result-
Process Exited with return value 3221225725
Don't use postincrement and similar in function calls.
Here's the problem condensed down to a really simple piece of code
#include <iostream>
int test(int n){
if(n == 1){
std::cout << "Function called!";
return test(n++);
}else{
return 0;
}
}
int main() {
test(1);
return 0;
}
Before you run this, ask yourself what you expect to happen here. Did it do what you thought?
When you run this you'll see that the code doesn't terminate properly. The output shows the function gets called infinitely many times, eventually the stack runs out of space and the program crashes.
You can see this code in action here: http://ideone.com/QL0jCP
In your program you have the same problem:
int func(int n)// say n = 4
{
if(a[n] <= a[n+1])//say this is true
{
func(n++); //this calls func(4) THEN increments n afterwards
}
This calls func with the same value over and over.
The solution is to not use postincrement or postdecrement in your function calls. These create hard to diagnose bugs as you have seen in this question. Just a simple func(n+1) is all you need. If you needed to use the variable later then just create an explicit variable to do that, it's much cleaner coding style (as this problem you ran into here shows).
After you fix this you'll need to fix your array bounds checking.
if(a[n] <= a[n+1])
If n is the last spot in the array you suddenly are trying to access one place past the end of the array, if you are lucky you get a segfault and a crash, if you are unlucky you get some bug that messes up your system that is hard to find. You want to check the values are valid.

visual studio breakpoints

I have a question about setting breakpoints in Visual Studio 2010 Professional.
In the struct below, I have an Update() function which, depending on certain conditions, updates the value of it's member i:
struct A
{
A(int i) : i(i) {}
void Update()
{
//Update i if some condition is met...
if(something)
i += 2;
}
int i;
};
int main()
{
A a(2);
//Update is usually called periodically...
a.Update();
return 0;
}
I would like to set a breakpoint to be hit when i equals 4. The only way I know how to do this is to change the Update() function like so:
void Update()
{
//Update i if some condition is met...
if(something)
i += 2;
if(i == 4)
int dummy = 1;
}
Now I can set a breakpoint on the line:
int dummy = 1;
And I will hit a breakpoint when i equals 4. Is there a cleaner or easier way to set a breakpoint in a situation like this? How do I do it without adding in the dummy code?
Set a breakpoint in the usual way with your mouse. This puts a big red dot in the left margin of your code. Now right-click on the big red dot with your mouse. You will see a list of ways you can change your breakpoint. Choose "Condition...". Then you can enter i==4 into the condition box. You'll have a breakpoint that breaks at that point when i is 4.

View Array contents in Qt Creator debugger

I am using Qt on Ubuntu. When I debug I only see the very first value of the array in Locals and Watchers. How can I view all the array contents?
struct node
{
int *keys;
void **pointers;
int num_keys;
struct node *parent;
int is_leaf;
struct node *nextLevelNode;
};
It shows only the first key value in the debugging window.
In Expression evaluator,
Try (int[10])(*myArray) instead of (int[10])myArray
Or, *myArray#10 instead of myArray#10
It shows only the first key value,in the debugging window
I presume you're referring to the pointer keys, declared with int *keys;
The debugger doesn't know that this is an array: all it knows is that this is a pointer to an int. So it can't know how many values you want it to display.
What I've found, using the Qt Creator 2.1.0 debugger on Ubuntu, is that the following code allows me to see all 5 values:
int array1[5];
array1[0] = 2;
array1[1] = 4;
array1[2] = 6;
array1[3] = 8;
array1[4] = 10;
Whereas with this code, the debugger only shows the first value, exactly as you describe.
int* array2 = new int[5];
array2[0] = 20;
array2[1] = 21;
array2[2] = 22;
array2[3] = 23;
array2[4] = 24;
Aside: of course, the above code would be followed by this, to avoid leaking memory:
delete[] array2;
Later: This Qt Developer Network Forum Post says that you can tell the debugger to display a pointer as an array:
In Locals and Watchers, context menu of your pointer’s entry, select “Watch Expression”. This creates a new watched expression below.
There, double click on the entry in the “Names” column, and add “#10” to display 10 entries.
This sounds like it should get you going.
Just right-click on your variable, and choose Change Value Display Format and check Array of 100 items.
In Qt for mac what worked for me was:
Add an expression evaluator for the desired variable (right click the variable on the debugger window then "Add expression evaluator for "var name here""
The array variable appears initially as a single value. Just change "var" to "var[start...end] and the array values appear.
Two dimensional arrays sometimes cannot be displayed that way. There is a work-around. First, declare a two-dimensional array as a one-dimensional array like this:
int width = 3;
int height = 4;
int* array2D = new int [width*height];
int x,y;
for(x=width-1;x>-1;x--)
for(y=height-1;y>-1;y--)
array2D[x*height + y] = -1; // mark a breakpoint here!
// add to expression evaluator: (int[3][4]) *array2D
delete [] array2D;
Then add (int[3][4]) *array2D to the expression evaluator. Unfortunately you have to index the array your self, but you can write a special-purpose inline function or use another encapsulation method to make it slightly cleaner.