Board index » delphi » Null Terminating Strings Part2

Null Terminating Strings Part2

Dear people,
      First I like to thank Mr Jochen Heyland for helping me with Null
Terminating Strings Part1. Now I have another question.  I am making an AI.
 I don't want the main program to poll the keyboard for input characters.
So I chained to keyboard to int09 to my own procedure.  Now all it has to
do every time the keyboard is pressed is add the character to the global
string G_string : pchar.  I have something similar to that written already.

  if keypressed then
     begin
        c := readkey;
        g_string := g_string + c;
     end;

Now that seems to be fine to me, but after reading the answer from Mr
Heyland, I am now faced with an potential bug.  Since the program will
clear the string when the loop gets to the procedure which anaylize the
string.  What happen if the loop takes too long to get to the string.  The
user could have inputed more than 65500 + characters, possibebly
overwriting potential memory locations.  Now people tell me if my worries
are just worries and not something to be worried about.

Note: the code segment above is just for simplicity.  Writing the actual
code that takes care of the keyboard interrupt will certainly make the life
of the reader more confuse than helping me.  Again Thanks in advance.

Oh yea thank you Mr Ron Muzzi for answering my Chaining ISR post and  I'm
sorry for somebody else that helped me out but I can't get the name off my
head.  Thanks!

 

Re:Null Terminating Strings Part2


In article <4afpfc$...@pipe5.nyc.pipeline.com>, con...@nyc.pipeline.com (Yu Kwan Cheng) says:
Quote

>Dear people,
>      First I like to thank Mr Jochen Heyland for helping me with Null
>Terminating Strings Part1.

No problem.

Quote
>Now I have another question.  I am making an AI.
> I don't want the main program to poll the keyboard for input characters.
>So I chained to keyboard to int09 to my own procedure.  Now all it has to
>do every time the keyboard is pressed is add the character to the global
>string G_string : pchar.  I have something similar to that written already.

>  if keypressed then
>     begin
>        c := readkey;
>        g_string := g_string + c;
>     end;

>Now that seems to be fine to me, but after reading the answer from Mr
>Heyland, I am now faced with an potential bug.  

Actually, I'm surprised you can use the "+" operator to add characters
to a pchar.  I'd assume that that would not work, so here's an alternative:

var
   G_string : pchar.  
   curlen : word;
begin
   GetMem(p,$FF00);
   curlen := 0;
   fillchar(p^,$FF00,#0);
   while keypressed do
     begin
        g_string[curlen]:= readkey;
        inc(curlen);
     end;

_or_

var
   G_string : pchar.  
begin
   GetMem(p,$FF00);
   fillchar(p^,$FF00,#0);
   while keypressed do
        g_string[StrLen(g_string)]:= readkey;

Quote
>Since the program will
>clear the string when the loop gets to the procedure which anaylize the
>string.  What happen if the loop takes too long to get to the string.  The
>user could have inputed more than 65500 + characters, possibebly
>overwriting potential memory locations.  Now people tell me if my worries
>are just worries and not something to be worried about.

Personally, I doubt that a user would enter more that a few hundred characters
even with just holding down, say, the space bar before they'd want to see
some reaction of the program but if you have enough space on the heap
this should work fine.

Quote
>Note: the code segment above is just for simplicity.  Writing the actual
>code that takes care of the keyboard interrupt will certainly make the life
>of the reader more confuse than helping me.  Again Thanks in advance.

Right you are!

Quote
>Oh yea thank you Mr Ron Muzzi for answering my Chaining ISR post and  I'm
>sorry for somebody else that helped me out but I can't get the name off my
>head.  Thanks!

Hope this helps!
Jochen

________________________________________________________________________
joch...@berlin.snafu.de                        71270.3...@compuserve.com
I post, therefore I am.

Other Threads