read function from CStrings tutorial does not work

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

read function from CStrings tutorial does not work

Post by HavocVulture » June 12th, 2018, 8:38 pm

Code: Select all

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;
}
I'm not sure what's wrong with that function but it doesn't work.

If I create a char array and pass it to read it inserts null terminators every other character.

Code looks like this:

Code: Select all

char name[10];
read(name, 10);
I type in "Bob", but the screen shows B o b and the array is populated with "\0B\0o\0b\0" followed by garbage.

HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

Re: read function from CStrings tutorial does not work

Post by HavocVulture » June 12th, 2018, 9:10 pm

I modified the function to filter out null terminators like this:

Code: Select all

void read( char* buf, int maxSize ) {
   const char* const pEnd = buf + maxSize;
   for ( char c = _getch(); c != 13 && (buf + 1 < pEnd); c = _getch(), buf++ ) {
    if(c != 0){
      _putch( c );
      *buf = c;
     }
   }
   *buf = 0;
}
But this left garbage data in buf. It looks like _getch() returns a bunch of nulls after every key press and buf keeps getting incremented.

So I modified the function like this and it seems to be working:

Code: Select all

	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;
	}
Regardless, _getch() is a terrible function and probably should never be used.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: read function from CStrings tutorial does not work

Post by chili » June 13th, 2018, 1:27 am

There is nothing wrong with the function per se, except that it is not platform-independent, and well, neither is Direct3D or any of the various functions needed to create windows and handle input messages :)

The main problem is that an update to Visual C++ compiler broke their implementation, which also happens to standard functions btw. It's one of the reasons why I recommended that people don't jump to VS2017 right away. To be honest though, I would not have expected this kind of bug at this point (I hope it would have stabilized by now).

Anyways it apparently works in Release mode, so for the sake of the tutorials, you can just run it in Release as a workaround (which is sub-optimal for doing this bullshit, but maybe it will teach you the skill of debugging without a debugger, which is super annoying but comes up) and all will be well.

https://developercommunity.visualstudio ... loops.html
Chili

HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

Re: read function from CStrings tutorial does not work

Post by HavocVulture » June 13th, 2018, 11:06 am

Glad it's a bug. I couldn't see what could be inserting those extra null terminators in the code.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: read function from CStrings tutorial does not work

Post by chili » June 13th, 2018, 1:30 pm

It's really annoying now because everybody is going to run into this issue (most ppl get 2017 these days i think). Gonna have to put a note in the wiki i guess :(
Chili

Post Reply