print the dynamic input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arnuld

    #1

    print the dynamic input

    I have created a program to print the input words on stdout. Input is
    taken dynamically from stdin. In each word, each input character is
    allocated dynamically. I have ran this program with a file containing a
    *single* word made of 25525500 letters and this program works fine on it.
    I will welcome any suggestions for improvement.


    /* * A program that will ask the user for input and then will print the words on stdout
    * and will also count the number of words entered.

    * Since I did not want to put any limitation on input size in this program, I have used
    * dynamic memory allocation to solve the problem. My intent in creating and then solving
    * this problem was purely of learning dynamic memory allocation in C (as defined by ANSI
    * standard) and nothing else.
    *
    * Reagrding storing input words, since there is no agreed definition of what a word is, I
    * have taken a very simple approach to it:
    *
    * Any contiguous collection of characters, containging anything
    * except single or multiple whitespace(s), is a word.
    *
    * VERSION 1.0
    *
    */


    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>


    enum { WORD_SIZE = 2, WORD_ALLOC_SIZE = 2 };
    enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;


    int get_single_word ( char**, unsigned long* );
    int allocate_new_me m( char**, char**, size_t* );


    int main( void )
    {
    char* pword = NULL;
    unsigned long letter_count = 0;
    size_t word_count = 0;

    while( (GSW_OK == get_single_word (&pword, &letter_coun t)) && ( *pword != 0) )
    {
    printf("You entered: [%s] - containing %ld alphabets\n", pword, letter_count);
    ++word_count;
    free( pword );
    }

    printf("word count = %d\n", word_count);

    return 0;
    }


    /* I know somehwere else in this program I should differentiate between the real End of File
    * (a.k.a no more input) and the not so real End of File (a.k.a error in input) and at clc I
    * got a suggestion to use getc(stdin) and feof() and ferror() for that but I am quite incapable
    * of using it. I lack the skill to apply that feature.
    */
    int get_single_word ( char** ppc, unsigned long* Lcnt )
    {
    unsigned ele_num = 0;
    int ch = EOF;
    size_t word_length = WORD_SIZE;
    char* word_begin = NULL;

    *ppc = malloc(word_len gth * sizeof(**ppc));
    word_begin = *ppc;


    if( NULL == *ppc ) return GSW_ENOMEM;


    while( (EOF != (ch = getchar())) && isspace(ch) )
    {
    continue; /* Leading whitespace */
    }


    if( EOF != ch )
    {
    *word_begin++ = ch;
    ele_num = 1;
    }


    while( (EOF != (ch = getchar())) && (! isspace(ch)) )
    {
    if( (word_length - 1) == ele_num++ )
    {
    if( allocate_new_me m( ppc, &word_begin, &word_length ) )
    {
    return GSW_ENOMEM;
    }
    }

    *word_begin++ = ch;
    }

    *word_begin = '\0';
    *Lcnt = ele_num;

    return GSW_OK;
    }


    int allocate_new_me m( char** moving_p, char** begin_p, size_t* pwl )
    {
    char* new_mem = NULL;

    new_mem = realloc( *moving_p, (WORD_ALLOC_SIZ E * (*pwl) * sizeof *new_mem));

    if( new_mem )
    {
    *begin_p = new_mem + ( *begin_p - *moving_p );
    *pwl *= WORD_ALLOC_SIZE ;
    *moving_p = new_mem;
    }
    else
    {
    *begin_p = '\0';
    return GSW_ENORESIZE;
    }

    return GSW_OK;
    }





    --

    my email is @ the above blog.
    Google Groups is now UnBlocked :)

  • Michael

    #2
    Re: print the dynamic input

    arnuld wrote:
    I have created a program to print the input words on stdout. Input is
    taken dynamically from stdin. In each word, each input character is
    allocated dynamically. I have ran this program with a file containing a
    *single* word made of 25525500 letters and this program works fine on it.
    I will welcome any suggestions for improvement.
    >
    >
    /* * A program that will ask the user for input and then will print the words on stdout
    * and will also count the number of words entered.
    >
    * Since I did not want to put any limitation on input size in this program, I have used
    * dynamic memory allocation to solve the problem. My intent in creating and then solving
    * this problem was purely of learning dynamic memory allocation in C (as defined by ANSI
    * standard) and nothing else.
    *
    * Reagrding storing input words, since there is no agreed definition of what a word is, I
    * have taken a very simple approach to it:
    *
    * Any contiguous collection of characters, containging anything
    * except single or multiple whitespace(s), is a word.
    *
    * VERSION 1.0
    *
    */
    >
    >
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    >
    >
    enum { WORD_SIZE = 2, WORD_ALLOC_SIZE = 2 };
    enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;
    >
    >
    int get_single_word ( char**, unsigned long* );
    int allocate_new_me m( char**, char**, size_t* );
    >
    >
    int main( void )
    {
    char* pword = NULL;
    unsigned long letter_count = 0;
    size_t word_count = 0;
    >
    while( (GSW_OK == get_single_word (&pword, &letter_coun t)) && ( *pword != 0) )
    {
    printf("You entered: [%s] - containing %ld alphabets\n", pword, letter_count);
    ++word_count;
    free( pword );
    }
    >
    printf("word count = %d\n", word_count);
    >
    return 0;
    }
    >
    >
    /* I know somehwere else in this program I should differentiate between the real End of File
    * (a.k.a no more input) and the not so real End of File (a.k.a error in input) and at clc I
    * got a suggestion to use getc(stdin) and feof() and ferror() for that but I am quite incapable
    * of using it. I lack the skill to apply that feature.
    */
    int get_single_word ( char** ppc, unsigned long* Lcnt )
    {
    unsigned ele_num = 0;
    int ch = EOF;
    size_t word_length = WORD_SIZE;
    char* word_begin = NULL;
    >
    *ppc = malloc(word_len gth * sizeof(**ppc));
    word_begin = *ppc;
    >
    >
    if( NULL == *ppc ) return GSW_ENOMEM;
    >
    >
    while( (EOF != (ch = getchar())) && isspace(ch) )
    {
    continue; /* Leading whitespace */
    }
    >
    >
    if( EOF != ch )
    {
    *word_begin++ = ch;
    ele_num = 1;
    }
    >
    >
    while( (EOF != (ch = getchar())) && (! isspace(ch)) )
    {
    if( (word_length - 1) == ele_num++ )
    {
    if( allocate_new_me m( ppc, &word_begin, &word_length ) )
    {
    return GSW_ENOMEM;
    }
    }
    >
    *word_begin++ = ch;
    }
    >
    *word_begin = '\0';
    *Lcnt = ele_num;
    >
    return GSW_OK;
    }
    >
    >
    int allocate_new_me m( char** moving_p, char** begin_p, size_t* pwl )
    {
    char* new_mem = NULL;
    >
    new_mem = realloc( *moving_p, (WORD_ALLOC_SIZ E * (*pwl) * sizeof *new_mem));
    >
    if( new_mem )
    {
    *begin_p = new_mem + ( *begin_p - *moving_p );
    *pwl *= WORD_ALLOC_SIZE ;
    *moving_p = new_mem;
    }
    else
    {
    *begin_p = '\0';
    return GSW_ENORESIZE;
    }
    >
    return GSW_OK;
    }
    >
    >
    >
    >
    >
    compiler warning

    abc.c: In function ‘main’:
    abc.c:46: warning: format ‘%d’ expects type ‘int’, but argument 2 has
    type ‘size_t’


    Suggested improvement:
    Change %d into %zd

    (I'm doing a complete rewrite of it.)

    Comment

    • Michael

      #3
      Re: print the dynamic input

      Michael wrote:
      arnuld wrote:
      >I have created a program to print the input words on stdout. Input is
      >taken dynamically from stdin. In each word, each input character is
      >allocated dynamically. I have ran this program with a file containing a
      >*single* word made of 25525500 letters and this program works fine on it.
      >I will welcome any suggestions for improvement.
      >>
      >>
      >/* * A program that will ask the user for input and then will print
      >the words on stdout
      > * and will also count the number of words entered.
      >>
      > * Since I did not want to put any limitation on input size in this
      >program, I have used
      > * dynamic memory allocation to solve the problem. My intent in
      >creating and then solving * this problem was purely of learning
      >dynamic memory allocation in C (as defined by ANSI * standard) and
      >nothing else.
      > *
      > * Reagrding storing input words, since there is no agreed
      >definition of what a word is, I * have taken a very simple approach
      >to it:
      > *
      > * Any contiguous collection of characters, containging
      >anything * except single or multiple whitespace(s), is a word.
      > *
      > * VERSION 1.0
      > *
      > */
      >>
      >>
      >#include <stdio.h>
      >#include <stdlib.h>
      >#include <ctype.h>
      >>
      >>
      >enum { WORD_SIZE = 2, WORD_ALLOC_SIZE = 2 };
      >enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;
      >>
      >>
      >int get_single_word ( char**, unsigned long* );
      >int allocate_new_me m( char**, char**, size_t* );
      >>
      >>
      >int main( void )
      >{
      > char* pword = NULL;
      > unsigned long letter_count = 0;
      > size_t word_count = 0;
      >>
      > while( (GSW_OK == get_single_word (&pword, &letter_coun t)) && (
      >*pword != 0) )
      > {
      > printf("You entered: [%s] - containing %ld alphabets\n", pword,
      >letter_count );
      > ++word_count;
      > free( pword );
      > }
      >>
      > printf("word count = %d\n", word_count);
      >>
      > return 0;
      >}
      >>
      >>
      >/* I know somehwere else in this program I should differentiate
      >between the real End of File
      > * (a.k.a no more input) and the not so real End of File (a.k.a error
      >in input) and at clc I * got a suggestion to use getc(stdin) and
      >feof() and ferror() for that but I am quite incapable * of using it.
      >I lack the skill to apply that feature. */
      >int get_single_word ( char** ppc, unsigned long* Lcnt )
      >{
      > unsigned ele_num = 0;
      > int ch = EOF;
      > size_t word_length = WORD_SIZE;
      > char* word_begin = NULL;
      >>
      > *ppc = malloc(word_len gth * sizeof(**ppc));
      > word_begin = *ppc;
      >>
      >>
      > if( NULL == *ppc ) return GSW_ENOMEM;
      > while( (EOF != (ch = getchar())) && isspace(ch) )
      > {
      > continue; /* Leading whitespace */
      > }
      >>
      >>
      > if( EOF != ch )
      > {
      > *word_begin++ = ch;
      > ele_num = 1;
      > }
      >>
      >>
      > while( (EOF != (ch = getchar())) && (! isspace(ch)) )
      > {
      > if( (word_length - 1) == ele_num++ )
      > {
      > if( allocate_new_me m( ppc, &word_begin, &word_length ) )
      > {
      > return GSW_ENOMEM;
      > }
      > }
      > *word_begin++ = ch;
      > }
      >>
      > *word_begin = '\0';
      > *Lcnt = ele_num;
      >>
      > return GSW_OK;
      >}
      >>
      >>
      >int allocate_new_me m( char** moving_p, char** begin_p, size_t* pwl )
      >{
      > char* new_mem = NULL;
      >>
      > new_mem = realloc( *moving_p, (WORD_ALLOC_SIZ E * (*pwl) * sizeof
      >*new_mem));
      >>
      > if( new_mem )
      > {
      > *begin_p = new_mem + ( *begin_p - *moving_p );
      > *pwl *= WORD_ALLOC_SIZE ;
      > *moving_p = new_mem;
      > }
      > else
      > {
      > *begin_p = '\0';
      > return GSW_ENORESIZE;
      > }
      >>
      > return GSW_OK;
      >}
      >>
      >>
      >>
      >>
      >>
      compiler warning
      >
      abc.c: In function ‘main’:
      abc.c:46: warning: format ‘%d’ expects type ‘int’, but argument 2 has
      type ‘size_t’
      >
      >
      Suggested improvement:
      Change %d into %zd
      >
      (I'm doing a complete rewrite of it.)
      /* new version */
      #include<stdio. h>
      #include<stdlib .h>
      #include<ctype. h>
      #include<stdboo l.h>

      size_t word_count=0,le tter_count=0;
      bool prev_whitespace =true;
      int c;

      void closing()
      {
      printf("] - containing %zd alphabets\n",le tter_count);
      }

      void whitespace_hand le()
      {
      if(!prev_whites pace)
      {
      closing();
      word_count++;
      letter_count=0;
      }
      prev_whitespace =true;
      }


      int main(void)
      {
      while((c=getcha r())!=EOF)
      {
      if(isspace(c))
      whitespace_hand le();
      else
      {
      if(prev_whitesp ace)
      printf("You entered: [");
      prev_whitespace =false;
      putchar(c);
      letter_count++;
      }
      }
      whitespace_hand le();
      printf("word count = %zd\n",word_cou nt);
      return 0;
      }

      Comment

      • vippstar@gmail.com

        #4
        Re: print the dynamic input

        On Oct 15, 3:19 pm, Michael <mich...@michae ldadmum.no-ip.orgwrote:
        <snip>
        compiler warning
        >
        abc.c: In function ‘main’:
        abc.c:46: warning: format ‘%d’ expects type ‘int’, but argument 2has
        type ‘size_t’
        >
        Suggested improvement:
        Change %d into %zd
        >
        (I'm doing a complete rewrite of it.)
        That's wrong - zd is for the corresponding integer type of size_t,
        which is not specified which one it is in the C99 standard.
        To print size_t, you'd use zu, zo, zx etc.

        Comment

        • Keith Thompson

          #5
          Re: print the dynamic input

          vippstar@gmail. com writes:
          On Oct 15, 3:19 pm, Michael <mich...@michae ldadmum.no-ip.orgwrote:
          <snip>
          >compiler warning
          >>
          >abc.c: In function ‘main’:
          >abc.c:46: warning: format ‘%d’ expects type ‘int’, but argument 2 has
          >type ‘size_t’
          >>
          >Suggested improvement:
          >Change %d into %zd
          >>
          >(I'm doing a complete rewrite of it.)
          >
          That's wrong - zd is for the corresponding integer type of size_t,
          which is not specified which one it is in the C99 standard.
          To print size_t, you'd use zu, zo, zx etc.
          You mean the corresponding *signed* integer type.

          It's also important to note that 'z' is not supported by all
          implementations . For greater portability, you can use "%lu" with a
          cast to unsigned long:

          printf("sizeof whatever = %lu\n", (unsigned long)sizeof whatever);

          Note that if size_t has a wider range than unsigned long, then this
          could produce incorrect output if "sizeof whatever" happens to exceed
          ULONG_MAX.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • s0suk3@gmail.com

            #6
            Re: print the dynamic input

            On Oct 15, 4:38 am, arnuld <sunrise@invali d.addresswrote:
            I have created a program to print the input words on stdout. Input is
            taken dynamically from stdin. In each word, each input character is
            allocated dynamically. I have ran this program with a file containing a
            *single* word made of 25525500 letters and this program works fine on it.
            I will welcome any suggestions for improvement.
            >
            /* * A program that will ask the user for input and then will print the words on stdout
               * and will also count the number of words entered.
            >
               * Since I did not want to put any limitation on input size in thisprogram, I have used
               * dynamic memory allocation to solve the problem. My intent in creating and then solving
               * this problem was purely of learning dynamic memory allocation inC (as defined by ANSI
               * standard) and nothing else.
            Well, that's very inconvenient. C's memory allocation functions aren't
            very handy for working with strings whose size must change
            dynamically, as you can see from the mess in this program. My
            suggestion, if you find yourself having to do this kind of thing
            often, would be to design a good 'string' type that handles memory
            (re)allocation issues under the hood.
               *
               * Reagrding storing input words, since there is no agreed definition of what a word is, I
               * have taken a very simple approach to it:
               *
               *       Any contiguous collection of characters, containginganyt hing
               *       except single or multiple whitespace(s), is a word.
               *
               * VERSION 1.0
               *
               */
            >
            #include <stdio.h>
            #include <stdlib.h>
            #include <ctype.h>
            >
            enum { WORD_SIZE = 2, WORD_ALLOC_SIZE = 2 };
            enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;
            >
            int get_single_word ( char**, unsigned long* );
            int allocate_new_me m( char**, char**, size_t* );
            >
            int main( void )
            {
              char* pword = NULL;
              unsigned long letter_count = 0;
              size_t word_count = 0;
            >
              while( (GSW_OK == get_single_word (&pword, &letter_coun t)) && ( *pword !=  0) )
                {
                  printf("You entered:  [%s] - containing %ld alphabets\n", pword, letter_count);
            ^^^

            That should be '%lu'.
                  ++word_count;
                  free( pword );
                }
            >
              printf("word count = %d\n", word_count);
            ^^

            And that should be '%zu'.
              return 0;
            >
            }
            >
            /* I know somehwere else in this program I should differentiate between the real End of File
             * (a.k.a no more input) and the not so real End of File (a.k.a error in input) and at clc I
             * got a suggestion to use getc(stdin) and feof() and ferror() for thatbut I am quite incapable
             * of using it. I lack the skill to apply that feature.
             */
            feof(stream) -- nonzero if a previous read operation on 'stream'
            encountered end-of-file
            ferror(stream) -- nonzero if a read or write error occurred on a
            previous operation on 'stream'
            int get_single_word ( char** ppc, unsigned long* Lcnt )
            {
              unsigned ele_num = 0;
              int ch = EOF;
              size_t word_length = WORD_SIZE;
              char* word_begin = NULL;
            >
              *ppc = malloc(word_len gth * sizeof(**ppc));
              word_begin = *ppc;
            >
              if( NULL == *ppc ) return GSW_ENOMEM;    
            >
              while( (EOF != (ch = getchar())) && isspace(ch) )
                {
                  continue; /* Leading whitespace */
                }
            >
              if( EOF != ch )
                {
                  *word_begin++ = ch;
                  ele_num = 1;
                }
            >
              while( (EOF != (ch = getchar())) && (! isspace(ch)) )
                {
                  if( (word_length - 1) == ele_num++ )
                    {
                      if( allocate_new_me m( ppc, &word_begin, &word_length ) )
                        {
                          return GSW_ENOMEM;
                        }
                    }
            >
                  *word_begin++ = ch;
                }
            >
              *word_begin = '\0';
              *Lcnt = ele_num;
            >
              return GSW_OK;
            >
            }
            >
            int allocate_new_me m( char** moving_p, char** begin_p, size_t* pwl )
            {
              char* new_mem = NULL;
            >
              new_mem = realloc( *moving_p, (WORD_ALLOC_SIZ E * (*pwl) * sizeof *new_mem));
            >
              if( new_mem )
                {
                  *begin_p  = new_mem + ( *begin_p - *moving_p );
                  *pwl     *= WORD_ALLOC_SIZE ;
                  *moving_p = new_mem;
                }
              else
                {
                  *begin_p = '\0';
            '*begin_p' has type 'char *'. The '\0' escape sequence has the value
            0, so you'll be assigning a null pointer to '*begin_p'. Are you sure
            that's what you intended to do?
                  return GSW_ENORESIZE;
                }
            >
              return GSW_OK;
            >
            }
            Sebastian

            Comment

            • Ben Bacarisse

              #7
              Re: print the dynamic input

              Michael <michael@michae ldadmum.no-ip.orgwrites:
              Michael wrote:
              >arnuld wrote:
              >>I have created a program to print the input words on stdout. Input is
              >>taken dynamically from stdin. In each word, each input character is
              >>allocated dynamically. I have ran this program with a file containing a
              >>*single* word made of 25525500 letters and this program works fine on it.
              >>I will welcome any suggestions for improvement.
              <snip>
              >(I'm doing a complete rewrite of it.)
              >
              /* new version */
              #include<stdio. h>
              #include<stdlib .h>
              #include<ctype. h>
              #include<stdboo l.h>
              >
              size_t word_count=0,le tter_count=0;
              bool prev_whitespace =true;
              int c;
              With every variable declared at file scope? This does not set a good
              style example!
              int main(void)
              {
              while((c=getcha r())!=EOF)
              {
              if(isspace(c))
              whitespace_hand le();
              else
              {
              if(prev_whitesp ace)
              printf("You entered: [");
              prev_whitespace =false;
              putchar(c);
              letter_count++;
              }
              }
              whitespace_hand le();
              printf("word count = %zd\n",word_cou nt);
              return 0;
              }
              OK, but the original is a stage in long-running exercise to write out
              a sorted list of words -- hence all the effort of storing them.

              --
              Ben.

              Comment

              • arnuld

                #8
                Re: print the dynamic input

                On Wed, 15 Oct 2008 09:51:45 -0700, s0suk3 wrote:
                >On Oct 15, 4:38 am, arnuld <sunrise@invali d.addresswrote:
                Well, that's very inconvenient. C's memory allocation functions aren't
                very handy for working with strings whose size must change
                dynamically, as you can see from the mess in this program. My
                suggestion, if you find yourself having to do this kind of thing
                often, would be to design a good 'string' type that handles memory
                (re)allocation issues under the hood.

                I will use C++ then. Don't get angry but I *have* to use C here. I am
                doing Socket Programming + whole lot of other communication Techie things
                that I am fixed on C most of the times. The actual cause is related with
                my job responsibility, not with the language itself.

                But does anyone think that I *should* create my own string library if
                strings are used often in my programs ?



                >      printf("You entered:  [%s] - containing %ld alphabets\n",
                >pword, letter_count);
                ^^^
                >
                That should be '%lu'.

                ouch!

                >  printf("word count = %d\n", word_count);
                ^^
                And that should be '%zu'.
                [arnuld@dune ztest]$ gcc4 -ansi -pedantic -Wall -Wextra word-count.c
                word-count.c: In function ‘main’:
                word-count.c:46: warning: ISO C90 does not support the ‘z’ printf length modifier
                [arnuld@dune ztest]$



                > *begin_p  = new_mem + ( *begin_p - *moving_p );
                >>  *pwl *= WORD_ALLOC_SIZE ;      
                > *moving_p = new_mem;     }
                >  else
                >    {
                >      *begin_p = '\0';
                '*begin_p' has type 'char *'. The '\0' escape sequence has the value 0,
                so you'll be assigning a null pointer to '*begin_p'. Are you sure that's
                what you intended to do?

                Oh.. no, I wonder why the program did not crash wit this bug. Changed to
                **begin_p = '\0';

                BTW, **begin = 0; will also be a good idea ? (because of implicit
                conversion)




                -- arnuld

                my email is @ the above blog.
                Google Groups is now UnBlocked :)

                Comment

                • arnuld

                  #9
                  Re: print the dynamic input

                  On Thu, 16 Oct 2008 01:09:10 +0100, Ben Bacarisse wrote:
                  OK, but the original is a stage in long-running exercise to write out
                  a sorted list of words -- hence all the effort of storing them.

                  Yippee...yaaa.. .. .. Hallelujah

                  Ben you remember it :)



                  --

                  my email is @ the above blog.
                  Google Groups is now UnBlocked :)

                  Comment

                  • Michael

                    #10
                    Re: print the dynamic input

                    arnuld wrote:
                    >On Wed, 15 Oct 2008 09:51:45 -0700, s0suk3 wrote:
                    >
                    >>On Oct 15, 4:38 am, arnuld <sunrise@invali d.addresswrote:
                    >
                    >Well, that's very inconvenient. C's memory allocation functions aren't
                    >very handy for working with strings whose size must change
                    >dynamically, as you can see from the mess in this program. My
                    >suggestion, if you find yourself having to do this kind of thing
                    >often, would be to design a good 'string' type that handles memory
                    >(re)allocati on issues under the hood.
                    >
                    >
                    I will use C++ then. Don't get angry but I *have* to use C here. I am
                    doing Socket Programming + whole lot of other communication Techie things
                    that I am fixed on C most of the times. The actual cause is related with
                    my job responsibility, not with the language itself.
                    >
                    But does anyone think that I *should* create my own string library if
                    strings are used often in my programs ?
                    >
                    >
                    >
                    >
                    >> printf("You entered: [%s] - containing %ld alphabets\n",
                    >>pword, letter_count);
                    > ^^^
                    >>
                    >That should be '%lu'.
                    >
                    >
                    ouch!
                    >
                    >
                    >> printf("word count = %d\n", word_count);
                    ^^
                    >And that should be '%zu'.
                    >
                    [arnuld@dune ztest]$ gcc4 -ansi -pedantic -Wall -Wextra word-count.c
                    word-count.c: In function ‘main’:
                    word-count.c:46: warning: ISO C90 does not support the ‘z’ printf length modifier
                    [arnuld@dune ztest]$
                    >
                    >
                    >
                    >
                    >> *begin_p = new_mem + ( *begin_p - *moving_p );
                    >> *pwl *= WORD_ALLOC_SIZE ;
                    >> *moving_p = new_mem; }
                    >> else
                    >> {
                    >> *begin_p = '\0';
                    >
                    >'*begin_p' has type 'char *'. The '\0' escape sequence has the value 0,
                    >so you'll be assigning a null pointer to '*begin_p'. Are you sure that's
                    >what you intended to do?
                    >
                    >
                    Oh.. no, I wonder why the program did not crash wit this bug. Changed to
                    **begin_p = '\0';
                    >
                    BTW, **begin = 0; will also be a good idea ? (because of implicit
                    conversion)
                    >
                    >
                    >
                    >
                    -- arnuld

                    my email is @ the above blog.
                    Google Groups is now UnBlocked :)
                    You can mix c++ code with c code easily.

                    Comment

                    • arnuld

                      #11
                      Re: print the dynamic input

                      On Thu, 16 Oct 2008 12:57:39 +0800, Michael wrote:
                      You can mix c++ code with c code easily.

                      Thanks and please don't quote the sigs. It makes it difficult to navigate
                      through the reply.


                      --

                      my email is @ the above blog.
                      Google Groups is now UnBlocked :)

                      Comment

                      • Nick Keighley

                        #12
                        Re: print the dynamic input

                        On 16 Oct, 05:21, arnuld <sunr...@invali d.addresswrote:
                        On Wed, 15 Oct 2008 09:51:45 -0700, s0suk3 wrote:
                        Well, that's very inconvenient. C's memory allocation functions aren't
                        very handy for working with strings whose size must change
                        dynamically, as you can see from the mess in this program. My
                        suggestion, if you find yourself having to do this kind of thing
                        often,
                        note: "often". I define "often" as twice or thrice.

                        would be to design a good 'string' type that handles memory
                        (re)allocation issues under the hood.
                        >
                        I will use C++ then. Don't get angry but I *have* to use C here.
                        why? I mean really why? Is there no C++ compiler
                        for your platform? Are your boss/collegues allergic to C++?
                        I am
                        doing Socket Programming + whole lot of other communication Techie things
                        that I am fixed on C most of the times. Do you have to interface
                        with existing C libraries.

                        I like C, but the "must use C" seems odd.

                        anything you can do in C you can do in C++. Do you have to interface
                        with existing C libraries?

                        The actual cause is related with
                        my job responsibility, not with the language itself.
                        ?
                        But does anyone think that I *should* create my own string library if
                        strings are used often in my programs ?
                        yes. Or at the very least hide all the nasty dynamic
                        memory stuff away in a library. Each thingy (function, library,
                        package etc.) should do one thing, and only one thing, well.
                        Sometimes called the Single Responsibility Principle (SRP).

                        You should be able to express your application's logic
                        without having dynamic memory allocation entangled in it.

                        <snip>

                        --
                        Nick Keighley

                        "Beware of bugs in the above code; I have only proved it correct, not
                        tried it."
                        -- Donald Knuth

                        Comment

                        • arnuld

                          #13
                          Re: print the dynamic input

                          On Wed, 15 Oct 2008 14:38:25 +0500, arnuld wrote:
                          I have created a program to print the input words on stdout. Input is
                          taken dynamically from stdin. In each word, each input character is
                          allocated dynamically. I have ran this program with a file containing a
                          *single* word made of 25525500 letters and this program works fine on it.
                          I will welcome any suggestions for improvement.
                          ...SNIP...

                          I ran this program and gave it some weired inputs and I get some
                          strange problems:


                          Ctrl + 4 -- programs says "Quit"
                          Ctrl + 2 -- program exits saying word_count is zero.

                          with other numbers it does not print anything on the screen but recognizes
                          that a word was entered. See the output for "Ctrl + 3" and "Ctrl + 5":

                          [arnuld@dune ztest]$ ./a.out
                          ^[
                          You entered: [- containing 1 alphabets
                          word count = 1

                          [arnuld@dune ztest]$ ./a.out
                          ^]
                          You entered: [] - containing 1 alphabets
                          word count = 1
                          [arnuld@dune ztest]$


                          Whats happening to the program ?



                          --

                          my email is @ the above blog.
                          Google Groups is now UnBlocked :)

                          Comment

                          • arnuld

                            #14
                            Re: print the dynamic input

                            On Wed, 15 Oct 2008 08:25:49 -0700, Keith Thompson wrote:
                            ...SANIP...
                            It's also important to note that 'z' is not supported by all
                            implementations . For greater portability, you can use "%lu" with a
                            cast to unsigned long:
                            >
                            printf("sizeof whatever = %lu\n", (unsigned long)sizeof whatever);
                            >
                            Note that if size_t has a wider range than unsigned long, then this
                            could produce incorrect output if "sizeof whatever" happens to exceed
                            ULONG_MAX.

                            Well, If 'z' not there in C90, and if we use then could be problems
                            caused by conversion of size_t to unsigned long then I think I better stop
                            using size_t for all. If I am going to cast it, then why use size_t in
                            first place ?



                            --

                            my email is @ the above blog.
                            Google Groups is now UnBlocked :)

                            Comment

                            • Ian Collins

                              #15
                              Re: print the dynamic input

                              arnuld wrote:
                              >On Wed, 15 Oct 2008 08:25:49 -0700, Keith Thompson wrote:
                              >
                              >...SANIP...
                              >
                              >It's also important to note that 'z' is not supported by all
                              >implementation s. For greater portability, you can use "%lu" with a
                              >cast to unsigned long:
                              >>
                              > printf("sizeof whatever = %lu\n", (unsigned long)sizeof whatever);
                              >>
                              >Note that if size_t has a wider range than unsigned long, then this
                              >could produce incorrect output if "sizeof whatever" happens to exceed
                              >ULONG_MAX.
                              >
                              >
                              Well, If 'z' not there in C90, and if we use then could be problems
                              caused by conversion of size_t to unsigned long then I think I better stop
                              using size_t for all.
                              You can't, size_t is the type of sizeof.

                              --
                              Ian Collins

                              Comment

                              Working...