--------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4300011 Date: 04/02/98 From: DARIN MCBRIDE Time: 06:45pm \/To: DAVID VAN HOOSE (Read 2 times) Subj: Reading Input From Stdin -> Moreover, the gets() function does not limit the transfer to the size -> of the supplied buffer, since it does not know that size. The use of -> fgets() instead is much safer. DVH> 1: fgets() is for use with files. What do you think stdin is? DVH> 2: It works, and works perfectly if you ever bothered to try it. It works perfectly in the pathological case where < 39 characters are entered. In other cases, it _can_ work fine, but this is not guaranteed. DVH> 3: It counts 0 thru 41 standardly. using cin.get() it uses 0 thru 40 DVH> because it already accounts for the NULL on the end. -> DH>char name[41]; This does NOT account for the "NULL" on the end. Nor does it account for the nul on the end. YOU have to account for it. The nul must appear anywhere from name[0] to name[40] since the buffer is only that large. DVH> Ok? Try it. You'll see that I am right. I use that all the time. Ever hear of the first internet worm? It was made possible by gets and its ability to overwrite large amounts of memory. Anyway, you are _incorrect_, and I would suggest going over your code a little more carefully. Try the following (it contains code that results in "undefined results", but most compilers won't complain... it is "bad code" but it's there to prove a point): #include #include #include void print10(char* p) { int i; for (i = 0; i < 10; ++i) { if (i != 0) printf(", "); printf("<%02X>", p[i]); } printf(" : "); for (i = 0; i < 10; ++i) { if (isprint(p[i])) printf("%c", p[i]); else printf("."); } printf("\n"); } int main() { char blank1[10] = {0}; /* all nuls */ char buffer[10] = {0}; /* all nuls */ char blank2[10] = {0}; /* all nuls */ printf("Sizes: blank1 <%lu>, buffer <%lu>, blank2 <%lu>\n", sizeof(blank1), sizeof(buffer), sizeof(blank2)); /* prove the all nuls: */ printf("before copy\n"); printf("blank1: "); print10(blank1); printf("buffer: "); print10(buffer); printf("blank2: "); print10(blank2); /* now copy in 10 characters. */ strcpy(buffer, "0123456789"); /* show the memory now */ printf("after copy\n"); printf("blank1: "); print10(blank1); printf("buffer: "); print10(buffer); printf("blank2: "); print10(blank2); /* blank out everything */ memset(blank1, 0, sizeof(blank1)); memset(buffer, 0, sizeof(buffer)); memset(blank2, 0, sizeof(blank2)); /* get some string */ printf("Enter a string (no more than 19 characters):"); gets(buffer); /* show the memory now */ printf("after gets\n"); printf("blank1: "); print10(blank1); printf("buffer: "); print10(buffer); printf("blank2: "); print10(blank2); return 0; } This compiled cleanly with gcc 2.7.2.1 (EMX 0.9c). The output was: [0] d:\tmp\src\g>t Sizes: blank1 <10>, buffer <10>, blank2 <10> before copy blank1: <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00> : .......... buffer: <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00> : .......... blank2: <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00> : .......... after copy blank1: <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00> : .......... buffer: <30>, <31>, <32>, <33>, <34>, <35>, <36>, <37>, <38>, <39> : 0123456789 blank2: <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00> : .......... Enter a string (no more than 19 characters):abcdefghijklmn after gets blank1: <6D>, <6E>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00> : mn........ buffer: <61>, <62>, <63>, <64>, <65>, <66>, <67>, <68>, <69>, <6A> : abcdefghij blank2: <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00>, <00> : .......... What I found interesting is the fact that it went BACK... I was expecting blank2 to be overwritten. DVH> Ok? Try it. You'll see that I am right. I use that all the time. David, I challenge YOU to try it. And post your code to prove it. --- * Origin: Tanktalus' Tower BBS (1:250/102) --------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4400000 Date: 04/02/98 From: RICKEY PARRISH Time: 09:16pm \/To: DARIN MCBRIDE (Read 2 times) Subj: SHELL DM> Welcome to the C++ echo, Rickey. The question you pose DM> is somewhat interesting, especially for the tried-and- DM> true DOS enthusiasts. However, while DOS can be on- DM> topic, it must be in C++. Hehe I dont know what I was thinking when I posted that question in here. Im writing the same program in C++ at the same time, but am not quite as good with C yet, so Im not that far into the program yet. Still getting the open/save routines done :) DM> Unfortunately, QBasic is not DM> on-topic here. I do beleive, however, that Fido does DM> have a basic echo (possibly even one for QBasic DM> specifically) where your question would not only be on DM> topic, but be more likely to find more people who DM> understand and can help with your code. Sorry about that. I wonder how many flames I'll get telling me its the wrong echo now? :) --- Maximus/2 2.02 * Origin: T-Shirts 'N Genes BBS Duncan Canada (250) 748-3408 (1:340/204) --------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4400001 Date: 04/01/98 From: MARK HOOVER Time: 07:16pm \/To: TIM HUTZLER (Read 2 times) Subj: Need More Memory "Bother," said Pooh to Tim Hutzler about Need More Memory. TH>TH>The "real" memory model limits *any* arrays to 65536 bytes. TH>TH>You will have to use 32-bit addressing. TH>MH>How would I accomplish this? Keep in mind, I'm compiling a DOS TH>MH>executable.... TH>That's not a problem. Also, you might consider a 'virtual' heap. You're going over my head here with both your ideas.....:) TH>You wrote 70% of the program, and *now* you discover that you can't TH>RAM 200k! Bummer!!! [grin] Didn't think about it at the time....:) TH>Just what are you try'n to do? Trying to read in nodelist.* CMPQwk 1.42 1960 Mirage Net Lie #10: I have a life outside the MirageNet. --- MirageNet HQ! * Origin: InnerSpace Mail Systems, Inc. (1:275/104) --------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4400002 Date: 04/02/98 From: MARK HOOVER Time: 02:47pm \/To: DARIN MCBRIDE (Read 2 times) Subj: 32-bit mode "Bother," said Pooh to Darin Mcbride about 32-bit mode. DM>believe that Borland requires an extention to create 32-bit DOS DM>executables.... but we should leave this up to the Borland wizards DM>(any left here?). I have Borland 4.52, 5.0, and 5.01 I don't *think* it'll do 32-bit DOS apps w/o an extention, but I'm not sure.... CMPQwk 1.42 1960 Mirage Net Mail: a lot faster than Canada Post. --- MirageNet HQ! * Origin: InnerSpace Mail Systems, Inc. (1:275/104) --------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4400003 Date: 04/03/98 From: BRUCE WEDDING Time: 12:03pm \/To: DAVID VAN HOOSE (Read 2 times) Subj: reading input from stdin (was: I'm an id12:03:4204/03/98 DVH> char name[40]; DVH> gets(name); DVH> name[40]='\0'; TT> That still leaves the problem that you're writing to memory that TT> isn't yours. Your array is only 40 characters long and you try to TT> access the 41st element! DVH> Actually it is correct. I don't know where you DVH> see anything wrong, but DVH> I have been making programs with that method for a while. DVH> Here is how I thought of it: It is not correct, and the fact that you have done this for a while is irrelevant. The array name is 40 characters long. These are accessed 0-39. When you write a 0 to name[40], you are writing to memory you don't own and it will blow up one day. Bruce --- Maximus/2 3.01 * Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000) --------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4400004 Date: 04/03/98 From: BRUCE WEDDING Time: 12:09pm \/To: BILL GODFREY (Read 2 times) Subj: Sorting linked list BG> Even better, sort the list stright away as you add BG> items in. Insert them in the right place to start BG> with. (That is, if it's appropiate to do so). I recall a post from Jerry Coffin that said it was much less efficient to sort a list as you build it. I don't have the specifics, but I do have faith in Jerry. I do have a utility program that builds a large list from a CSV spreadsheet and after reading it into the list, I sort is with the LL merge sort in Snippets. Seems to work fine and sure is easier than writing my own. Bruce Wedding bruce@scimisys.com --- Maximus/2 3.01 * Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000) --------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4400005 Date: 04/02/98 From: CHRISTOPHER BUTLER Time: 07:38pm \/To: ALL (Read 2 times) Subj: Help! Hello All! How would I go about writing an EditBox class? I'd like to be able to make it simply a class that has "events" (keypresses) passed to a handling function, so that I can have more than one thing on the screen at once, with one of them having focus. I've tried a lot of things, from doubly-linked lists of String classes to Vectors of char... :/ I've looked through snippets, but to no avail.. Can anyone give my any hints/code? I'm using DJGPP, and would also like to be able to use it on other versions of GCC (ie Linux, etc), so I'd prefer not to have many OS-specific things :) Christopher E-Mail: chris@sandy.force9.co.uk --- FMailX32 1.22 * Origin: Death Butler BBS, +44-1582-620141, 24 Hours (2:257/135) --------------- FIDO MESSAGE AREA==> TOPIC: 203 C++ Ref: F4400006 Date: 03/29/98 From: CHRISTOPHER BUTLER Time: 11:23am \/To: SEBASTIAN NOZZI (Read 2 times) Subj: Exceptions Hi Sebastian! Hey, Sebastian wasn't it you that was talking about Exceptions on 23 Mar 98? Yeah... you were talking to All, weren't you? SN> What are exceptions and what do they have to do with C++? What are SN> they used for? Exceptions are a form of error handling, they are (in my experience anyway), simple classes, defined a bit like this: class GeneralError { public: GeneralError(int err): error(err) { } int errNo() { return err; } char *strError() { return strerror(err); } private: int error; }; When an error occurs, the function where it happens will throw the exception: char *myFunc() { // ... if (something went wrong) throw GeneralError(5); } Then, in the calling function: try { foo = myFunc(); } catch (xGeneralError err) { cerr << err.strError() << endl; } In C, without exceptions, the same function might go like this: char *myFunc() { /* ... */ if (something went wrong) return NULL; } If this isn't handled in the calling function, then it can leed to all kinds of mayhem (segmentation faults, etc). However, if an exception is not caught, the program calls the abort() function, and exits cleanly. ----------------------------------------------------------------------------- Chris Butler [email: chrisb@sandy.force9.co.uk] ----------------------------------------------------------------------------- I don't think anyone would ever say "A shower will scare you to death before it kills you." -- Abby Franquemont-Guillory on alt.sysadmin.recovery --- FMailX32 1.22 * Origin: ... The Death Butler BBS, +44-1582-620141 ... (2:257/135)