TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DAVID VAN HOOSE
from: GEORGE WHITE
date: 1998-04-03 08:47:00
subject: reading input from stdin

Hi David,
You wrote to Tom Torfs:
DVH>DVH> char name[40];
DVH>DVH> gets(name);
DVH>DVH> name[40]='\0';
DVH>TT> That still leaves the problem that you're writing to memory that
DVH>TT> isn't yours. Your array is only 40 characters long and you try to
DVH>TT> access the 41st element!
DVH>Actually it is correct.  I don't know where you see anything wrong, but
DVH>I have been making programs with that method for a while.
It isn't correct, unfortunately :-(. "char name[40];" declares an array
with 40 elements, but as C++ uses zero based counting this gives valid
array elements name[0] through name[39]. name[40] is beyond the end of
the declared array. As Tom says, it's the 41st element.
You have just been lucky so far! I was writing one byte beyond the end
of a string in one of my programs due to a bug, and when I fixed it a
number of random corruptions and problems went away (fortunately none of
the customers entered full lines of text and so it hadn't provided any
on site problems).
George
 * SLMR 2.1a * Wastebasket: Something to throw things near.
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.