Thread: Buffered input

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    46

    Buffered input

    Simple question concerning input. Say I declare a buffer to be of size 1024, and I'm sure that the input will almost always be less than that.
    My question is if I plan to work a lot with that buffer, will I need to initialize it to null characters to avoid getting garbage after processing?
    C code. C code run. Run code, run...please!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i would always initialise a buffer completely. Any help against bugs creeping in has to be a good thing.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yeah, better to be safe than sorry.

    >> C code. C code run. run code, run...please! <<

    That is great! I wish I thought of that.

    ---Garfield
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    What's the best way to do that? As far as I understand, the best way is to use a for loop.
    Code:
    for(i = 0; i < 1024; i++){buf[i] = '\0';}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If all you are doing is passing that buffer to fgets for example, then clearing it before hand (or even before each fgets call) adds nothing.

    fgets always adds a \0, and there is never anything useful after that (nor should you be looking there either), so there shouldn't be a problem.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Well, yes, that would be the way to do it...if you used a char aray []. But, if you were to use a char *, you would just need to do:
    Code:
    char *string;
    
    /***init string***/
    
    string = NULL;
    All you have to do is init a pointer as NULL.

    --Garfield
    1978 Silver Anniversary Corvette

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    @Salem - Yes, I use fgets to fill my char arrays. That's nice to know, thank you.

    @Garfield - My question was about char arrays, not char pointers. Since either you initialize a char pointer to a string when you create it, or use it as a pointer to an existing variable, the question of whether or not to initialize it to nulls is moot. Though I always initialize my pointers to null pointers when I create them so that I can't accidentally try to use a value that I don't want without knowing.

    Though I do have a followup for the answer you brought up Garfield. If I have a ragged array, is there a way to clear it to nulls or is there no need?
    char *array[10];
    C code. C code run. Run code, run...please!

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm. I haven't tried this, but I think this will initialize all the elements of the array to NULL.

    char * array[10] = {};
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    And that would work for any array type?
    char array[1024] = {};
    etc...

    Anyway, I was under the impression that any array declared with global or extern scope will automatically be initialized to all 0, and anything with local or automatic scope is undefined until explicitly initialized.
    With that in mind, how does placing nothing inside the {} cause the array to fill with NULL?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That would be
    char array[1024] = { 0 }; // 1024 '\0' bytes

    or
    char * array[10] = { 0 }; // 10 NULL pointers

  11. #11
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    hm... salem, is there a standard on uninitialized values of a particular type? i know that for many of the base types there is no value rectification... also, if you know what's to be in the buffer, when the input procedures end, all you'd need to do is append the final null in order to get correct stoppage... but it depends on your implementation...
    hasafraggin shizigishin oppashigger...

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is there a standard on uninitialized values of a particular type?
    Given
    <any_base_type_or_pointer_to_base_type> array[1024] = { 0 };
    The compiler will cast the 0 to be a proper 0.0 in the case of floats/doubles, and a proper NULL in the case of pointer types.

    Note that this is different than filling the array with say memset.
    http://www.eskimo.com/~scs/C-faq/q7.31.html

    If you've got K&R - 2nd Ed, read A8.7

  13. #13
    Unregistered
    Guest
    there is a few ways to initialize a string to 0...

    char str[1024] = "\0";

    and if you want to set it to totally null afterwards:
    memset(str, 0, 1023);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM