Difference between debug and release when viewed in debugger

I'm viewing variables using the debugger. In debug builds everything in the code below appears as I expect it to, but when I switch to release builds I'm getting strange results. Why?

#include <iostream>

void say_hello(int argc, char* argv[])//In release mode argc has different values from 124353625 to 36369852 when viewed in the debugger
{
    std::cout << "In say_hello()\n";
}

int main(int argc, char* argv[])
{
    say_hello(3,argv);//when instead of literal I enter "argc" everything is ok.
    return 0;
}

Thanks for help.

Upvotes: 2

Views: 491

Answers (3)

Suma
Suma

Reputation: 34413

In Release version the code is optimized and many values are kept in registers. Debugger does not know how to access and display such values.

See What does “Optimize Code” option really do in Visual Studio?

Upvotes: 0

JoeG
JoeG

Reputation: 13192

The results of your program are correct in both Release and Debug mode.

When you view the variables in the debugger in an optimized build you shouldn't expect them to hold the 'correct' values. In this case your compiler has optimized away any trace of the argc and argv from say_hello because they're not used.

Upvotes: 2

Rob Kennedy
Rob Kennedy

Reputation: 163287

Since you're not using those parameters in your program, you must be trying to observe their values in the debugger. But, again since you're not using them in your program, the compiler is free to do whatever it wants with their values. It may remove them entirely, leaving the debugger with nothing but gibberish to display when you ask for each parameter's value. If you change your optimization and debug-information settings, you may see different results.

Upvotes: 2

Related Questions