Thread: getch and ungetch without buffer

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    84

    getch and ungetch without buffer

    Hi,

    I want to have getch() and ungetch() without a static buffer.
    What I have to change in following code from K&R to get that?

    Code:
    #define BUFSIZE 100
    
       char buf[BUFSIZE];    /* buffer for ungetch */
       int bufp = 0;         /* next free position in buf */
    
       int getch(void)  /* get a (possibly pushed-back) character */
       {
           return (bufp > 0) ? buf[--bufp] : getchar();
       }
    
       void ungetch(int c)   /* push character back on input */
       {
           if (bufp >= BUFSIZE)
               printf("ungetch: too many characters\n");
           else
               buf[bufp++] = c;
       }

  2. #2
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    I want to realize it with malloc and realloc, but I don`t know how.Please can someone help me...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > I want to have getch() and ungetch() without a static buffer.

    Why, exactly? I ask because 1) it may not be necessary and 2) what you want to accomplish alters how you would go about it.

    > I want to realize it with malloc and realloc, but I don`t know how.

    Change char buf[BUFSIZE]; to char *buf; for starters. Though now you need to call an initialization function which calls malloc, and that puts an onus on the calling code of your library to remember the initialization function. Or if you're lucky enough to be writing your own implementation of C, that initialization can be done invisibly before main is called. Otherwise you're boned, and this becomes a question of whether you want to complicate the library interface and if the benefit is worth the complication.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    677
    You don't need to initialize anything before main is called if you dynamically allocate space when it's needed. Keep track of the buffer's current size (it can start at 0) and enlarge the buffer only when you need to put more data into it than will fit. You can use realloc() for all allocations, even when buf is initially NULL.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > You don't need to initialize anything before main is called if you dynamically allocate space when it's needed

    Again bringing us to the question of what the OP is trying to accomplish. If one needs more than a handful of character's worth of buffer in ungetch, my first inclination is to say that the design of the program (rather than getch/ungetch) is broken and needs fixing.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getch and ungetch functions Unable to understand
    By sleftar in forum C Programming
    Replies: 8
    Last Post: 11-16-2015, 10:56 AM
  2. chapter 4 K&R, getch() and ungetch()
    By blob84 in forum C Programming
    Replies: 1
    Last Post: 04-28-2011, 04:38 AM
  3. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  4. ungetch()
    By 0927 in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2002, 10:35 PM
  5. differences between getch(), getche(), and ungetch()
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2002, 02:08 AM

Tags for this Thread