0

I got this function from a tutorial

void read( char* buf, int maxSize ) {
   const char* const pEnd = buf + maxSize;
   for ( char c = _getch(); c != 13 && (buf + 1 < pEnd); c = _getch(), buf++ ) {
      _putch( c );
      *buf = c;
   }
   *buf = 0;
}

It filled buf with null terminators after each character was entered.

I had to modify it like this to make it work:

void read( char* buf, int maxSize ) {
    const char* const pEnd = buf + maxSize;
    char c = 0;

    while ( c != 13 && (buf < pEnd) ) {
        c = _getch();
        if ( c != 0 ) {
            _putch( c );
            *buf = c;
            buf++;
        }
    }

    *buf = 0;
}

What is wrong with _getch()? Why does it return null terminators constantly? Even in the working function if I step through it, I can see _getch() returning '\0' 3 or 4 times after each character is typed.

EDIT -- I'm using Visual Studio 2017. While this looks like C code, it's because the tutorial series starts by teaching cstrings before moving on to std::string.

Legion
  • 3,922
  • 8
  • 51
  • 95
  • 1
    `_getch()` is not part of standard C or standard C++. What compiler and operating system are you using? Does the compiler have documentation? – Pete Becker Jun 12 '18 at 21:16
  • Are you working with C or C++? This looks like C code, not C++. – NathanOliver Jun 12 '18 at 21:17
  • @PeteBecker Visual Studio 2017, Windows 10 – Legion Jun 12 '18 at 21:21
  • @NathanOliver While this looks like C code, it's because the tutorial series starts by teaching cstrings before moving on to `std::string`. – Legion Jun 12 '18 at 21:21
  • Try using the standard ones (which I believe are just `getch()` and `putch()`). – J. Doe Jun 12 '18 at 21:23
  • @J.Doe MS actively marks it (and many others) as POSIX-deprecated, in favor of the ISO counterparts (which all being with underscore), a warning message I'm confident the OP is tired of seeing with every line of seemingly trivial code that uses such facilities [See this Q&A and its related links](https://stackoverflow.com/questions/814975/getch-is-deprecated). – WhozCraig Jun 12 '18 at 21:27
  • @WhozCraig Correct (about the warnings) – Legion Jun 12 '18 at 21:29
  • `_getch()` returns `int`, not `char`. Try changing the declaration of `c`. – Barmar Jun 12 '18 at 21:33
  • @WhozCraig I checked the link and it said that MS was pushing non-standard, non-platform independent functions that aren't actually a problem to use... – J. Doe Jun 12 '18 at 21:33
  • @Barmar `int` and `char` are usually interchangeable for one-byte numbers. As long as he's not planning to catch any non-ANSI characters, he should be fine – J. Doe Jun 12 '18 at 21:35
  • @J.Doe I thought so, too, but it seems like he's getting extra bytes from the int that aren't in the char. – Barmar Jun 12 '18 at 21:38
  • @WhozCraig -- `_getch` is not an "ISO counterpart"; it's a Microsoft invention; the name avoids encroaching on the user's namespace (names that begin with an underscore are reserved to the implementation for use in the global namespace). – Pete Becker Jun 12 '18 at 21:39

1 Answers1

1

It turns out there's a bug in Visual Studio 2017 with _getch() which causes null terminators to be inserted after every character. If you switch from debug to release mode the bug goes away.

See here: https://developercommunity.visualstudio.com/content/problem/252047/something-wrong-with-getch-in-loops.html

Legion
  • 3,922
  • 8
  • 51
  • 95