-3

While debugging a program in Visual Studio 2013, I found something strange. In one expression where I use min(), strange values appear. In the Immediate Window in VS, I try this:

min(W, H)
35 '#'
W
291
H
682

Duh-what?? What is the explanation of this?

DarenW
  • 16,549
  • 7
  • 63
  • 102
  • 5
    Looks like some conversion to an 8-bit type is performed: 35 dec == 23 hex, and 291 dec == 123 hex, 682 dec == 2AA hex. When truncating at 8 bit, we get `min(0x23, 0xAA) == 0x23`. – dyp Feb 03 '15 at 23:42
  • 2
    What code produced that output? – Keith Thompson Feb 03 '15 at 23:43
  • 6
    ... and the definition of `min()` is? – abligh Feb 03 '15 at 23:44
  • 2
    The fact that the `35` is followed by `'#'` is also an indication, in addition to what @dyp commented, that the debugger sees something involving `char` (or maybe `unsigned char`) types. –  Feb 04 '15 at 00:11
  • As far as I know, the VS debugger's Immediate window doesn't evaluate macros, so you must have a function implementation of `min()` that's being used. The SDK's implementation of `min()` is a macro, and `std::min()` is a template that I also believe would be unavailable to the Immediate window evaluation (at least it's not obvious to me how to get the Immediate window to evaluate something with `std::min()`). – Michael Burr Feb 04 '15 at 00:13
  • @MichaelBurr: I wonder if `using namespace std;` is involved – Mooing Duck Feb 04 '15 at 00:17

1 Answers1

2

min is treating its inputs as chars. That's why it shows the character '#' next to 35, because the ascii value of 35 is #. The numbers you are giving are out of the range of a char, so they are wrapping. You're really doing min(291%256, 682%256) which is min(35, 170) which is obviously 35.

David
  • 27,652
  • 18
  • 89
  • 138
  • I found out today that I wasn't including header for min(), max(). I did have a special definition for my own byte type. Why it was used when given ints (32-bit) w/o any warnings, I dunno. But now I have math.h and other headers, so problem solved. – DarenW Feb 05 '15 at 05:28