10
\$\begingroup\$

Which values of x and y will cause a crash with some C compilers?

int f(int x, int y) {
    return (y==0) ? 0 : (x/y);
}
\$\endgroup\$
  • \$\begingroup\$ Since C's ternary operator shortcuts, I would say none would. This question doesn't seem to fit the format for this site, which is focused on program puzzles and code golf. See the faq for details codegolf.stackexchange.com/faq. \$\endgroup\$ – Steven Rumbalski Jan 12 '12 at 14:48
  • \$\begingroup\$ This isn't code golf, but is a puzzle. There is an answer, and it's just a couple of numbers. \$\endgroup\$ – ugoren Jan 12 '12 at 14:51
  • \$\begingroup\$ I stand corrected. \$\endgroup\$ – Steven Rumbalski Jan 12 '12 at 14:59
  • 2
    \$\begingroup\$ Actually, judging by the K&R book, this function really must never crash. But by the ANSI C standard, the behavior in the particular crashing case is undefined, and with x86 compilers it crashes. \$\endgroup\$ – ugoren Jan 12 '12 at 15:05
  • 1
    \$\begingroup\$ @dmckee, If you give the right answer, you're the winner. What cretirion could be more clear and objective? There's only one answer (or do you have another example?) \$\endgroup\$ – ugoren Jan 12 '12 at 18:10
7
\$\begingroup\$

-2147483648 (INT_MIN) and -1

#include <stdio.h>
#include <limits.h>
int f(int x, int y) {
    return (y==0) ? 0 : (x/y);
}
int main() {
    int r = f(INT_MIN, -1);
    printf("%d\n", r);
    return 0;
}

$ gcc -Wall division.c && ./a.out # => zsh: floating point exception ./a.out

\$\endgroup\$
  • \$\begingroup\$ Indeed. Though this should give a warning, because 2147483648 isn't a valid integer. \$\endgroup\$ – ugoren Jan 12 '12 at 14:57
  • 1
    \$\begingroup\$ Yes, that's why I used INT_MIN after, to use a valid int. I guess the reason is 2147483648 is not a valid int, since INT_MAX is 2^31-1 with 32-bit int. \$\endgroup\$ – eregon Jan 12 '12 at 14:59
  • \$\begingroup\$ Ah. Two's complement. I missed that. \$\endgroup\$ – Steven Rumbalski Jan 12 '12 at 15:00
  • \$\begingroup\$ Yes, it should compile cleanly with INT_MIN (which is -2147483648). \$\endgroup\$ – ugoren Jan 12 '12 at 15:02
3
\$\begingroup\$

The right answer is already given, but I immediately thought about Microsoft Pex.

Pex automatically generates test suites with high code coverage. Right from the Visual Studio code editor, Pex finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage. Microsoft Pex is a Visual Studio add-in for testing .NET Framework applications

After adding your puzzle in the sandbox site, it finds the answer in a few seconds, the same as eregons answer. (click ask pex)

Note: it does it in C#, but the language is not really relevant.

  • x: int.MinValue
  • y: -1
  • Exception: OverflowException
  • Message: Arithmetic operation resulted in an overflow.
\$\endgroup\$
  • 1
    \$\begingroup\$ Nice. It surely doesn't brute-force it, because it wouldn't end in a few seconds. I guess someone in MS realized that numbers around 0 and MAX_INT are always interesting. \$\endgroup\$ – ugoren Jan 17 '12 at 15:19
  • \$\begingroup\$ Hopefully it's a little more clever than that. It might look at (x/y) and know that INT_MIN, -1, 0 etc. are all problem cases for that expression, and try to reverse engineer a way to produce those values at the time of evaluation. \$\endgroup\$ – Clueless Jan 18 '12 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.