[comp.lang.c] Quick C Strange Problems

Shekar_Narayanan.SV@Xerox.COM (03/08/88)

Hello there,

	I am using QuickC for some development at my leisure at home. I am having the
strange problems as described below:

1) With character pointers. The code reads like this.

char *getstring(col, row, len)
int col, row, len;
{
	char *temp, *s;
	char ch;
	int count;

	gotoxy(col, row);
	clearxy(col, row);	/* clear upto end of row */
	gotoxy(col, row):
	temp = s;
	count = 0;
	for (;;)
	{
		if (count > len) return temp;
		ch = getkey(); /* get a key from user */
		if (ch == ESC) return NULL;
		switch (ch)
		{
			case CR:
				*s = ch;
				return temp;
				break;
			case BS:
				if (count > 0)
				{
				*(s --) = '\0';
				col --;
				count --;
				}
				break;
			default:
				*s = ch;
				s++;
				col++;
				count ++;
				break;
		}
		gotoxy(col, row);
		putchar(ch);
	}
}

In this code what I expect to see is that the return in "temp" should contain
the char array that "s" accumulated in the process. Instead, while debugging I
found that "temp" was pointing only to the last character in the array. Also the
char pointers temp and s are pointing to garbage string of characters from
nowhere at the first line of the program. I am not sure what is wrong here. Do I
need to forcefully do strcpy(temp, ""); to initialise the char pointers
everytime.

2) If I use the C library functions int86 to manipulate cursor and other goodies
via dos interrupt and somewhere else in the program I use direct video ram
access to write to screen then things go crazy. Is there any limitation that is
not documentated in QuickC.

3) Finally, I somehow got my program running and compiling in memory in QC
environment and when I create and exe image from QC and try to run it from dos
command line the screen just freezes and I have to reboot. I had only the stack
checking and language extensions options turned on in compiling the program. I
am not sure what is wrong.

4) I am also interested in some more information on QC library calls for Memory
resident programs  using the calls _setvec _getvec  etc . There are absolutely
no examples in the run time library documentation. 

	Would appreciate any input.

	Thanks in advance,

	Shekar				

barmar@think.COM (Barry Margolin) (03/09/88)

In article <12170@brl-adm.ARPA> Shekar_Narayanan.SV@Xerox.COM writes:
>1) With character pointers. The code reads like this.
>
>char *getstring(col, row, len)
>int col, row, len;
>{
>	char *temp, *s;
>	char ch;
>	int count;
>
>	temp = s;
>	count = 0;
>	for (;;)
>	{
>		if (count > len) return temp;
>		ch = getkey(); /* get a key from user */
>		if (ch == ESC) return NULL;
>		switch (ch)
>		{
>			case CR:
>				*s = ch;
>				return temp;
>				break;
[rest of program edited out.]

The problem is that you never allocated any storage for your return
string.  Since you never initialized it, s is pointing to an
effectively random part of memory when this function is entered.  When
you then store through it you could be overwriting anything, which
would explain why your program freezes.

The first fix is to allocate the storage you need, with

	s = malloc (len+1); /* Leave room for the trailing '\0' */

Another problem with your program is that you don't put a trailing
'\0' at the end of the string you return.  In the CR case, before the
"return" statement you should do

	*(s+1) = '\0';

Finally, in the case of ESC, you should free the string before
returning NULL.

Minor point: why do you check for ESC in the "if" statement, rather
than making it one of the switch cases?

Barry Margolin
Thinking Machines Corp.

barmar@think.com
uunet!think!barmar