   Better version of gets() "I hope"
  Hi all,I've been reading and have seen messages like, the 'gets' command is dangerous, and stuff like that. Though, on the bright side I might have the answer to all those problems, during my experience working with the Quake engine I've learned to re-write C functions from scratch so here is a rough draft of gets() but in better shape:  #include <stdlib.h>#include <stdio.h>#pragma warning (disable : 4018)// signed/unsigned mismatchchar *getstring(char *buf, size_t size) {    intc, y;    buf = (char*) malloc (size);    y = 0;    while ((c = getchar()) != EOF)    {   // Make sure the buffer doesn't exceed defined size   if( y < (size-1) )   {  putchar(c);  buf[y] = char(c);  y++;   }    }    buf[y] = '\0';    return buf;}// Example of how to use thisint main(){    char buffer[1024];    // New: set max size (25) or do sizeof(buffer) for max size    printf("Type in your name: ");    getstring(buffer, 25);    printf("Your name is: %s\n", buffer);    return(0);}I hope this is a big help to some of you, plus if you'd like to leave any feedback about this function or if you are experiencing any problems with it, feel free to inform me.- [BDS]StackOverflow    [edited by - BlueDev on February 2, 2004 10:51:40 PM] 
  that code makes no sense. ;  mega memory leakage if(!buf)  return NULL;   buf = (char*) malloc (size);youre saying, if the buffer has info in it already, allocate more space without deleteing the old.   [edited by - Mulligan on February 2, 2004 9:58:54 PM];  This actually worked in your test?  I think it''d be ok without the malloc. A smaller issue: why not just write sign-correct code, instead of suppressing the warning? Your approach will bite you with non-trivial code. ;  You do realize that when you do getstring(buffer, 25);, you are not modifying buffer at all, don''t you ?   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  This is a pointless re-write. The C standard library already includes fgets(). Instead of your getstring() function you can just use fgets(buffer, 25, stdin). ;  Alright,Well I removed the "if(!buf) return NULL;" line. Though, I have to allocate memory to write to "char *buf" because I can''t write to each byte as long as it doesnt already exist. If you want, remove the malloc() but its only there to make sure you don''t exceed the amount of memory for the character. Esp, if you aren''t writing to a char [] like I am as "char buffer[1024]" in main()- [BDS]StackOverflow ;  Ah, I see... Think it has a few bugs but oh well I''ll see if I can fix it up sometime, just saw it work partially but now I see it isn''t working to perfectly.- [BDS]StackOverflow ;quote:Original post by Fruny    You do realize that when you do getstring(buffer, 25);, you are not modifying buffer at all, don't you ?   ... or make it to accept just a 'size' parameter since he's not modifying anything but returning the pointer.In the end the client must remember to call free() for each getstring(). Phew... I would rather use fgets().  [EDIT] SiCrane suggested fgets()... he's right. I've forgotten that gets() don't need the 'size' parameter - dangerous. [edited by - DerekSaw on February 3, 2004 2:01:11 AM];quote:Original post by DerekSaw  Phew... I would rather use gets().   *cough* fgets() *cough*