Thread: char *str

  1. #1
    Unregistered
    Guest

    char *str

    Code:
    #include <stdio.h>
    
    int main (void)
    {
     char *str;
    
     fgets (str, 1000000, stdin);
    
     return 0;
    }
    What if user enters 1000000 bytes from his/her keyboard?

    Is there any allocated memory to place them?

    Thanks

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    No there is not - you'll have to use malloc to allocate the 1000000 bytes.

  3. #3
    Unregistered
    Guest
    But program works without allocating memory ? !

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    It may work this time - and even next time, but I guarentee you that it will eventually fail. What you have is an unitialised pointer, which could be pointing anywhere in memory - then you write the user input at this location in memory.
    If you are entering only a couple of chars you will probably get away with it, but eventually your program will attempt to overwrite memory that doesn't belong to it. If you allocate the memory and the allocation succeeds, then you may write data at this location.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
     char *str;
    
     if((str = (char *) malloc(sizeof(char) * 101)) == NULL)
     {
    	 printf("Error: Unable to allocate memory");
    	 exit(1);
     }
    
     fgets (str, 100, stdin);
    
     return 0;
    }
    PS: I thought 1000000 was a bit of overkill (pretty long string ) so I went for something smaller.
    Last edited by foniks munkee; 03-08-2002 at 07:30 AM.

  5. #5
    Unregistered
    Guest

    Smile

    Thank you, foniks !

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    No problem, but I made a really bad mistake..

    free(str);

    Once your finished with the memory that you have borrowed from the system, you must return it!

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1
    Ok ok.

    I'm new to all this mumbo jumbo - what if I want to read an arbitrarily long string? As in - there is not limit to how long it is (within a theoretical limit)... no max_length definitions, etc.

    how would I go about doing that? I've been trying it myself, and looking around the place, but to no avail.

    cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM