-3

In K&R book, there is itoa code in Chapter 3 on 64th page. I tried to compile the code, but i was not successful. Here is the code:

#include <iostream>
#include <conio.h>
using namespace std;
void itoa(int, char*);

int main(void) {
    _getch();
    char arr[100];
    itoa(-18,arr);
    _getch();
    return 0;
}

void itoa(int n, char* s) {
    int i, sign;
    if ((sign = n) < 0) {
        n = -n;
    }
    i = 0;
    do {
        s[i++] = n % 10 + '0';
    } while ((n / 10) > 0);
    if (sign < 0) s[i++] = '-';
    s[i] = 0;
    //reverse(s);
}

Output:

Access violation on line 25, which is: s[i++] = n % 10 + '0';

asu
  • 1,875
  • 17
  • 27
Silidrone
  • 1,471
  • 4
  • 20
  • 35
  • 2
    `(n / 10)` --> `(n /= 10)` – BLUEPIXY Dec 03 '16 at 19:19
  • 3
    That is not a compilation error. – Lightness Races in Orbit Dec 03 '16 at 19:19
  • Sure that `s[i++]` doesn't go out of bounds? – πάντα ῥεῖ Dec 03 '16 at 19:20
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Dec 03 '16 at 19:20
  • 1
    `s[i++]` will go out of bounds because the loop never ends if it was branched once, since `n` is not assigned. (see BLUEPIXY's comment) – asu Dec 03 '16 at 19:20
  • `#include ` isn't c++ standard. May be just use `getc();` instead of `_getch();`. – πάντα ῥεῖ Dec 03 '16 at 19:23
  • 1
    @πάνταῥεῖ: It's C code semi-hacked so it can sort of be compiled by a C++ compiler. K&R is a C book; all the programs are C programs. – Jonathan Leffler Dec 03 '16 at 19:38
  • Note that this algorithm doesn't work when the value of the argument is `INT_MIN` and the computer is 'normal' (uses 2's complement arithmetic). – Jonathan Leffler Dec 03 '16 at 19:38
  • @JonathanLeffler Was `conio.h` ever part of the c standards? – πάντα ῥεῖ Dec 03 '16 at 19:40
  • @πάνταῥεῖ: No, `` is restricted to Windows (DOS?); it is not a part of the C, C++ or POSIX standards. The `using namespace std;` is completely bogus as C code too, of course. (IIRC, the programs in K&R 2nd Edn were tested with a C++ compiler, circa 1988, because Standard C compilers were not yet available). – Jonathan Leffler Dec 03 '16 at 19:42
  • @JonathanLeffler So what about my comment you're arguing in particular? Or did I misunderstood? – πάντα ῥεῖ Dec 03 '16 at 19:43
  • 1
    @πάνταῥεῖ: To the extent I'm arguing, I'm arguing with you and/or the world that the code from K&R is not intended to be C++ code (so discussing whether `` is part of the C++ standard is tangential). I intensely dislike the dual-tagging of the question, but I can't work out what's the best way to fix it (other than delete it since the problem is mainly that the code in the question isn't an accurate copy of the code in the book). – Jonathan Leffler Dec 03 '16 at 19:45
  • @Jonathan _"since the problem is mainly that the code in the question isn't an accurate copy of the code in the book"_ Well, I have to admit it was the last millennium I took a look at that book :D. – πάντα ῥεῖ Dec 03 '16 at 19:48

1 Answers1

2

itoa has an endless loop that exceeds the bounds of arr after 100 digits are generated. The loop is not modifying n so it can terminate when n reaches 0. Change the while condition to use n /= 10 instead of n / 10.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770