Thread: size of buffer

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    size of buffer

    I'm asking for a stream of numbers and want to store each number in an array. The number of #'s is variable, so I want to check how many #'s a user has entered so I can allocate the necessary amount of memory for my array.

    Thanks

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can either allocate a lot of memory and shoot for the best, or create a queue if you need to be more exact.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can't put the cart before the horse. You have to plan in advance for some number of responses, and then act accordingly when that number is exceeded. That number could be 1 or 50 million - you don't know. My point being is that whatever you choose, when you get one more response than you planned for, you'll have to regroup.

    The lazy person's choice might be to plan for 100 responses. If that or fewer are received, you are done. If more than 100 are received, then you'll need to pull out the heavy artillery and get some storage from the heap and create a new array that is larger than your initial pick (by some degree larger - you're choice).

    Another way might be to get a hunk of heap from the get-go, and instead of defining it as an array, just treat it as an array, and when it's not big enough any more, realloc() that hunk to some amount larger than the first, and keep going with adding more entries.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Mmm.. makes sense, but how do I know I've read all the entries?

    for instance:

    numbers: 1 2 3 4 5 6 7 8

    code:
    a loop?...
    scanf("%d", store in array);
    ...

    How do I know when to stop?

    Thanks

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The read routine you use will set the EOF (end of file) indicator, and you will test it and act accordingly.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The queue idea that I mentioned would be something more like:

    Example:
    Code:
    #include <stdlib.h>
    
    struct item
    {
      int value;
      struct item *next;
    };
    
    int push(struct item **q, int value)
    {
      struct item *node = malloc(sizeof(*node)), *tmp;
    
      if(!node)
        return EXIT_FAILURE;
    
      node->value = value;
      node->next = 0;
    
      if(*q)
      {
        for(tmp = *q; tmp->next; tmp = tmp->next)
          ;
        tmp->next = node;
      } else
        *q = node;
    
      return EXIT_SUCCESS;
    }
    
    int pop(struct item **q, int *r)
    {
      struct item *tmp;
    
      if(!r || !q)
        return EXIT_FAILURE;
    
      if(!*q)
        return EXIT_FAILURE;
    
      tmp = *q;
      *q = (*q)->next;
      *r = tmp->value;
      free(tmp);
      
      return EXIT_SUCCESS;
    }
    
    int main(int argc, char **argv)
    {
      struct item *q = 0;
      int i;
    
      for(i = 1; i < argc; i++)
        push(&q, atoi(argv[i]);
    
      while(pop(&q, &i) == EXIT_SUCCESS)
        printf("&#37;d ", i);
    
     return EXIT_SUCCESS;
    }
    Last edited by master5001; 10-28-2008 at 01:12 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Great, thanks

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I dunno who that was directed at, but I will give it to you, Todd. You were a little more distinct than I was on this one.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Let's give it to Salem. I think Salem needs it.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    True. Salem takes more crap than any of us. Todd, did you read the post within my link. I am so going to reference it everytime someone gets overly frustrated. At least I got that particular student's attitude turned around.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I wish somedays I could just "give up" on a problem I'm working on at work!!
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Largest screen buffer size?
    By Ash1981 in forum C Programming
    Replies: 2
    Last Post: 01-30-2006, 04:31 AM
  5. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM