Thread: fscanf's auto-allocation for strings

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    fscanf's auto-allocation for strings

    I hear that fscanf is evil and fgets outshines it in spades, however, fscanf has an addictive advantage (or so I've been told) in that it auto-allocates memory for string arguments; which makes me feel all warm and fuzzy inside! However its behaviour is oddly different when handling strings that are part of an array...
    If I create a string:
    Code:
    char *mystring;
    and then pass this null pointer to fscanf:
    Code:
    fscanf(stdin, "%s", mystring);
    It will automatically allocate space for "mystring".
    HOWEVER, if I now create an array of (lets say 10) strings (aka 10 char pointers):
    Code:
    char **mystrings = (char **)calloc(10, sizeof(char *));
    And then pass one (any one, but lets use the first) of these pointers to fscanf, all of sudden it doesnt want to auto allocate memory for it anymore!:
    Code:
    //causes memory/access error
    fscanf(stdin, "%s", *mystrings);
    I don't get it... "mystrings" is an array of char pointers, so "*mystrings" is a char pointer (namely the first one) just like any other char pointer, yet because it belongs to an "array", somehow fscanf won't treat it like a regular char pointer (as it did in the first example with "mystring").
    Additionally, if I go so far as to explicitly assign any pointer (lets use the first) in the array of pointers to a pre-allocated pointer:
    Code:
    char *mystring;
    char **mystrings = (char **)calloc(10, sizeof(char *));
    //explicitly assign the first pointer of the array to another pointer
    *mystrings = mystring;
    fscanf(stdin, "%s", *mystrings);
    Now it magically works again!!
    Leaving my question.... is there something about scanf that it absolutely needs a pointer to have an explicit memory address (i.e. "mystring"), as opposed to it being a memory address pointed to by a pointer (i.e. "*mystring"), in order for it to auto-allocate memory for it??
    OR.. have I been misinformed,... does fscanf really allocate memory for strings, or does it simply just stomp over any memory it can find on the program stack??
    Last edited by @nthony; 07-01-2006 at 02:38 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't know where you heard that fscanf() automatically allocates memory for you, but it doesn't. scanf()/fscanf() need to be passed a buffer big enough to hold the string.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fscanf has an addictive advantage (or so I've been told) in that it auto-allocates memory for string arguments;
    Wow, who told you that crud?
    No doubt someone who tried it on DOS, found it didn't crash and promptly proclaimed that it must be true.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If fscanf did allocate memory for you, it would have to change the char*s value. Which would involve passing the char* itself by reference. But no, you pass it by value. And you would have to free that memory. See. Next time think before blindly believing what your friend/teacher/pet llama says.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jafet
    Which would involve passing the char* itself by reference. But no, you pass it by value.
    Strictly speaking, everything in C is passed by value, there is no such thing as 'by referene'. But other than wording, you're correct.
    Quote Originally Posted by jafet
    And you would have to free that memory.
    Maybe it comes with its own garbage collection too!


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Maybe it comes with its own garbage collection too!
    Maybe Microsoft performed a hostile takeover of the standards committee and instituted their usual "fixes" of "unsafe" standard library components.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    They already did, no? They called it C#
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    They already did, no? They called it C#
    Some say C++/CLI, but it is probably both.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Thanks for clearing up that misconception! Indeed, after closer inspection it would appear that fscanf is happy to quietly overwrite memory that does not belong to the string.. apparently they pay the TA's $22 an hour to NOT know what their talking about at my university....

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >apparently they pay the TA's $22 an hour to NOT know what their talking about at my university....
    Aren't TAs students? That's a bit like the blind leading the blind unless you get extremely lucky. Fortunately, this forum will correct that particular problem. You can verify anything that doesn't seem quite right by asking the members here who have years of experience and know the language inside and out.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Prelude
    Fortunately, this forum will correct that particular problem. You can verify anything that doesn't seem quite right by asking the members here who have years of experience and know the language inside and out.
    I wouldn't necessarily back that statement, at least not entirely. All members here are as capable of making mistakes as any other human being. Yes, the forum can help. And having multiple members means more chance of mistakes being recognised, and the people here are willing to acknowledge mistakes. But assuming members here are perfect can be just as dangerous as trusting novice TAs: so don't be afraid to check on what you're told.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I wouldn't necessarily back that statement, at least not entirely.
    There comes a point where you have to trust your source, otherwise you'll be stuck in an endless loop of trying to confirm every source you find. Lack of confidence is just as dangerous as lack of knowledge. In my experience, this forum has several members who are authoritative enough in their knowledge of C to answer most questions and catch most problems. For the harder problems, there are better sources, but a beginner doesn't often stumble on those problems until he's experienced enough to know those sources.

    Naturally, I would trust the word of Chris Torek over Salem due to sheer reputation, much like I would trust the word of Salem over some unnamed TA who seems to have no idea how pointers are passed to functions. Then again, if I don't believe Salem, I'll be sure to double check his claims...if only because it's fun to correct people who are a great deal smarter than I am.
    My best code is written with the delete key.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The whole point of forums is the fact that your releasing your questions to several reliable sources at one.If someone says something wrong, five other people will jump on it immediately.

    I could have told you that fscanf() doesn't auto allocate memory, but that doesn't mean I know more than your TA. As you said, we're all capable of mistakes and I'm sure I've made plenty he/she would be able to correct. Although, that one was a doozy.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 06:43 PM
  4. Are auto pointers any good?
    By cunnus88 in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2007, 11:07 AM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM