Thread: Simulating the string class - your comments on this, please

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I first read one character into int c and I need to allocate memory for it. That is why I am using "sizeof(c)". What am I supposed to put there then?

  2. #17
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    'c' is an int. You need this to test for EOF, but, you want to store a char.

    Also, you ought to store the return value of realloc in a temporary as Quzah suggested to avoid that problem if it fails.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'm not operating at 100%, but I might work with this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       char  *temp, *input = NULL;
       size_t i = 0;
       fputs("Type something: ", stdout);
       fflush(stdout);
       for ( ;; )
       {
          int c = getchar();
          if ( c == '\n' || c == EOF )
          {
             break;
          }
          temp = realloc(input, i + 2/* incoming char and null */);
          if ( temp )
          {
             input = temp;
             input[i++] = c;
             input[i] = '\0';
          }
          else
          {
             free(input);
             break;
          }
       }
       if ( input )
       {
          puts(input);
       }
       free(input);
       return 0;
    }
    
    /* my output
    Type something: hello world
    hello world
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by caduardo21
    I first read one character into int c and I need to allocate memory for it. That is why I am using "sizeof(c)". What am I supposed to put there then?
    Read my post again. I explain why you don't need sizeof(c).

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dave_Sinkula
    Code:
          else
          {
             free(input);
             break;
          }
       }
       if ( input )
       {
          puts(input);
       }
       free(input);
       return 0;
    }
    Skip the free call there, and just break. Or, set 'input' to NULL after the free. (edit: Oh, and move the 'free' call inside the last if check.) Otherwise you'll end up trying to free your pointer twice.

    But since we're both not operating at full capacity tonight... (heads off for another brew)

    [edit=2]
    Quote Originally Posted by cwr
    Read my post again. I explain why you don't need sizeof(c).
    Hehe. We'll get him eventually.
    [/edit]


    Quzah.
    Last edited by quzah; 08-25-2005 at 09:35 PM.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    This is 7:15 in the morning, I just came off the shower and I think I finally got it...
    I don't need sizeof(c) because that will allocate four bytes and all I need is one byte because a character is always a byte. Is that correct?

  7. #22
    ---
    Join Date
    May 2004
    Posts
    1,379
    I think somebody might have already mentioned this but I didn't read the whole thread so,

    Code:
    if (input == NULL)
            return 1;
    input wont be NULL because NULL is never assigned to it.

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >input wont be NULL because NULL is never assigned to it.
    And if malloc() fails, what will be assigned to it?

  9. #24
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by caduardo21
    This is 7:15 in the morning, I just came off the shower and I think I finally got it...
    I don't need sizeof(c) because that will allocate four bytes and all I need is one byte because a character is always a byte. Is that correct?
    Yeah, kind of like when I said "You are still originally malloc'ing sizeof(int), but you only need 1 byte for the first char." above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM