Author Topic: Using Debug & stepping thru each line, not showing when in one function  (Read 2819 times)

Offline VicM

  • Single posting newcomer
  • *
  • Posts: 6
Hi,
I'm using Code::Blocks 20.03 on a Win 7 Pro box.  I'm using Brian Overland's C++ Without Fear 3rd Ed.
I copied one of his examples into Code::Blocks and tried to run it.  It did not produce the intended results.  So I rechecked that I had copied the code correctly, and I had. 

I then decided to run the code one line at a time and set up watches on some of the variables to see if I could detect a problem.  The code has two functions.  One that has a couple of for loops and the other that swaps two variables.

When stepping thru the main code, it enters the first function when it's invoked.  The second function is called from the first function, the function executes when called, but does not indicate that it went thru it step-wise.  And therefore the variables in that function never show up in the watch list.  They just say: Not available in current context.

I'm at a loss to understand what's going on here.

There was an error in the printed code on the page of the book where a variable was declared with the name of "lowest".  But in the code "lowest" was never used, however "low" was.  It produced a compile error.  So I changed "lowest" to "low" in the declaration and the code compiled.

Thanks,
Vic

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Using Debug & stepping thru each line, not showing when in one function
« Reply #1 on: February 07, 2022, 12:17:02 am »
Variables in the debugger are always as visible as in the code, so you can not see one variable from one function in a other (because this variables do not exist outside of the function). You have to use global variables, or step into the function that defines the variables you want to observe.

I do not understand your problem fully. But i think you have to use "Step into" in your funtion, where you want to observe the variables.
For example
Code
void func1()
{
  int var1 = 4;
  int var2 = 4;
}

void func2()
{
->  func1();
}


You place a breakpoint on func1 in func2 (like displayed by arrow) then, when the debugger stops at the breakpoint (the yellow arrow appears in the line) you use the function "Step into", this will then bring the debugger one line into func1();

Offline VicM

  • Single posting newcomer
  • *
  • Posts: 6
Re: Using Debug & stepping thru each line, not showing when in one function
« Reply #2 on: February 07, 2022, 06:58:20 pm »
Thank you BlueHazzard.  Using the step into (Shift-F7) allowed me to see what those variables are doing.  But I'm still a little confused because when I just use the next line (F7) it moves into the first function when it's called from the main but not when that function calls the second.
Anyhow, your reply was extremely helpful.
Thanks again.
Vic