TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DAVID VAN HOOSE
from: TOM TORFS
date: 1998-03-30 23:51:00
subject: reading input from stdin (was: I`m an id23:51:1703/30/98

* Reply to a message in personal_mail.
David Van Hoose wrote in a message to Tom Torfs:
 DVH> Refer to a message I wrote to Roger Scudder.  I made a typo in my
 DVH> example.  Again, this is what it is supposed to be:
 DVH> char name[40];
 DVH> gets(name);
 DVH> name[40]='\0';
That still leaves the problem that you're writing to memory that isn't yours. 
Your array is only 40 characters long and you try to access the 41st element!
 DVH> gets() does not using Borland, GNU, Microsoft, or Watcom
 DVH> compilers, put a '\0' (NULL) on the end to signify that it has
                             ^^^^ as I said in the previous reply, this use 
of NULL is not recommendable in C++ and may even produce errors in C; you 
mean NUL
 DVH> filled the character array to the max.
gets() has no clue about the array size; as soon as you exceed the array 
bounds undefined behaviour occurs, so anything may happen. Solution: never 
use gets().
greetings,
Tom
tomtorfs@village.uunet.be
--- timEd/2 1.10+
---------------
* Origin: 80X86 BBS 32-15-24.62.32 V.34/V.FC (24h/24h) (2:292/516)

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™.