realloc

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

    realloc

    Hi all :
    My code below :

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

    char *cat(char *s, const char *t)
    {
    char *tmp;

    if (s == NULL)
    tmp = realloc(s,strle n(t) + 1);
    else
    tmp = realloc(s,strle n(s) + strlen(t) + 1);
    if (tmp == NULL) {
    fputs("realloc error",stderr);
    exit(1);
    }
    while (*tmp++); // search for '\0' and stop
    tmp--; //the position of '\0'
    while (*tmp++ = *t++) ;
    s = tmp;
    return s;
    }

    int main()
    {
    char *tmp;
    char *s;

    s = NULL;
    tmp = cat(s,"oh");
    printf("%s\n",t mp);
    tmp = cat(s,"haha");
    printf("%s\n",t mp);
    return 0;
    }
    my problem is when I run the program the reslut is some blanks. I just
    wrote a small routine like strcat and check the realloc and tmp pointer
    carefully but found nothing.

  • baumann@pan

    #2
    Re: realloc


    Roy wrote:[color=blue]
    > Hi all :
    > My code below :
    >
    > #include <stdio.h>
    > #include <string.h>
    > #include <stdlib.h>
    >
    > char *cat(char *s, const char *t)
    > {
    > char *tmp;
    >
    > if (s == NULL)
    > tmp = realloc(s,strle n(t) + 1);
    > else
    > tmp = realloc(s,strle n(s) + strlen(t) + 1);
    > if (tmp == NULL) {
    > fputs("realloc error",stderr);
    > exit(1);
    > }
    > while (*tmp++); // search for '\0' and stop[/color]

    the above statement is not right, when s = null, what do you expect the
    *tmp is?
    [color=blue]
    > tmp--; //the position of '\0'
    > while (*tmp++ = *t++) ;
    > s = tmp;
    > return s;
    > }
    >
    > int main()
    > {
    > char *tmp;
    > char *s;
    >
    > s = NULL;
    > tmp = cat(s,"oh");
    > printf("%s\n",t mp);
    > tmp = cat(s,"haha");
    > printf("%s\n",t mp);
    > return 0;
    > }
    > my problem is when I run the program the reslut is some blanks. I[/color]
    just[color=blue]
    > wrote a small routine like strcat and check the realloc and tmp[/color]
    pointer[color=blue]
    > carefully but found nothing.[/color]

    Comment

    • Roy

      #3
      Re: realloc

      baumann@pan wrote:[color=blue]
      > Roy wrote:[color=green]
      > > Hi all :
      > > My code below :
      > >
      > > #include <stdio.h>
      > > #include <string.h>
      > > #include <stdlib.h>
      > >
      > > char *cat(char *s, const char *t)
      > > {
      > > char *tmp;
      > >[/color][/color]
      correct here:
      if (s == NULL) {
      tmp = realloc(s,strle n(t) + 1);
      while(*tmp++ = *t++); // copy *t to *tmp.
      } else {[color=blue][color=green]
      > > tmp = realloc(s,strle n(s) + strlen(t) + 1);
      > > if (tmp == NULL) {
      > > fputs("realloc error",stderr);
      > > exit(1);
      > > }
      > > while (*tmp++); // search for '\0' and stop[/color]
      >[/color]
      the above statement is not right, when s = null, what do you expect
      the
      *tmp is?
      Now handled the s=null but still blanks...[color=blue][color=green]
      > > tmp--; //the position of '\0'
      > > while (*tmp++ = *t++) ;
      > > }[/color]
      > s = tmp;
      > return s;[color=green]
      > > }
      > >
      > > int main()
      > > {
      > > char *tmp;
      > > char *s;
      > >
      > > s = NULL;
      > > tmp = cat(s,"oh");
      > > printf("%s\n",t mp);
      > > tmp = cat(s,"haha");
      > > printf("%s\n",t mp);
      > > return 0;
      > > }
      > > my problem is when I run the program the reslut is some blanks. I[/color]
      > just[color=green]
      > > wrote a small routine like strcat and check the realloc and tmp[/color]
      > pointer[color=green]
      > > carefully but found nothing.[/color][/color]

      Comment

      • pete

        #4
        Re: realloc

        Roy wrote:[color=blue]
        >
        > Hi all :
        > My code below :
        >
        > #include <stdio.h>
        > #include <string.h>
        > #include <stdlib.h>
        >
        > char *cat(char *s, const char *t)
        > {
        > char *tmp;
        >
        > if (s == NULL)
        > tmp = realloc(s,strle n(t) + 1);
        > else
        > tmp = realloc(s,strle n(s) + strlen(t) + 1);
        > if (tmp == NULL) {
        > fputs("realloc error",stderr);
        > exit(1);
        > }
        > while (*tmp++); // search for '\0' and stop
        > tmp--; //the position of '\0'
        > while (*tmp++ = *t++) ;
        > s = tmp;
        > return s;
        > }
        >
        > int main()
        > {
        > char *tmp;
        > char *s;
        >
        > s = NULL;
        > tmp = cat(s,"oh");
        > printf("%s\n",t mp);
        > tmp = cat(s,"haha");
        > printf("%s\n",t mp);
        > return 0;
        > }[/color]

        char *cat(char *s, const char *t)
        {
        char *tmp;

        if (s == NULL) {
        tmp = realloc(s, strlen(t) + 1);
        *tmp = '\0';
        } else {
        tmp = realloc(s, strlen(s) + strlen(t) + 1);
        }
        if (tmp == NULL) {
        fputs("realloc error",stderr);
        exit(EXIT_FAILU RE);
        }
        s = tmp;
        while (*tmp++ != '\0') {
        ;
        }
        tmp--;
        while ((*tmp++ = *t++) != '\0') {
        ;
        }
        return s;
        }

        --
        pete

        Comment

        • Al Bowers

          #5
          Re: realloc



          pete wrote:
          [color=blue][color=green]
          >>#include <stdio.h>
          >>#include <string.h>
          >>#include <stdlib.h>
          >>[/color][/color]
          [color=blue][color=green]
          >>int main()
          >>{
          >> char *tmp;
          >> char *s;
          >>
          >> s = NULL;
          >> tmp = cat(s,"oh");
          >> printf("%s\n",t mp);
          >> tmp = cat(s,"haha");
          >> printf("%s\n",t mp);
          >> return 0;
          >>}[/color]
          >
          >
          > char *cat(char *s, const char *t)
          > {
          > char *tmp;
          >
          > if (s == NULL) {
          > tmp = realloc(s, strlen(t) + 1);
          > *tmp = '\0';[/color]

          Here is a slight flaw. You do not want to assign to *tmp
          BEFORE you have checked tmp value for a possible NULL.
          [color=blue]
          > } else {
          > tmp = realloc(s, strlen(s) + strlen(t) + 1);
          > }
          > if (tmp == NULL) {
          > fputs("realloc error",stderr);
          > exit(EXIT_FAILU RE);
          > }
          > s = tmp;
          > while (*tmp++ != '\0') {
          > ;
          > }
          > tmp--;
          > while ((*tmp++ = *t++) != '\0') {
          > ;
          > }
          > return s;
          > }
          >[/color]

          This definition will abort the program if dynamic allocation
          fails. Another less virulent act on the executable would be
          give a signal to the caller, via the return value, or some
          other means, that the function was not successful in the
          operation.

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

          int cat(char **s1, const char *s2);

          int main(void)
          {
          char *s = NULL;

          if(cat(&s,"Hell o"))
          {
          if(cat(&s," and Goodbye"))
          printf("s = \"%s\"\n",s) ;
          else puts("Memory allocation failure");
          }
          else puts("Memory allocation failure");
          free(s);
          return 0;
          }

          int cat(char **s1, const char *s2)
          {
          int ret = 0; /* value would indicate failure */

          if(s1 && s2)
          {
          char *tmp;
          size_t sz = *s1?strlen(*s1) +strlen(s2)+1:s trlen(s2)+1;
          if((tmp = realloc(*s1,sz) ) != NULL)
          {
          if(!*s1) *tmp = '\0';
          strcat(tmp,s2);
          *s1 = tmp;
          ret = 1; /* value would indicate success */
          }
          }
          return ret;
          }



          --
          Al Bowers
          Tampa, Fl USA
          mailto: xabowers@myrapi dsys.com (remove the x to send email)
          Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


          Comment

          • pete

            #6
            Re: realloc

            Al Bowers wrote:[color=blue]
            >
            > pete wrote:
            >[color=green][color=darkred]
            > >>#include <stdio.h>
            > >>#include <string.h>
            > >>#include <stdlib.h>
            > >>[/color][/color]
            >[color=green][color=darkred]
            > >>int main()
            > >>{
            > >> char *tmp;
            > >> char *s;
            > >>
            > >> s = NULL;
            > >> tmp = cat(s,"oh");
            > >> printf("%s\n",t mp);
            > >> tmp = cat(s,"haha");[/color][/color][/color]

            Probably would be a better demonstration with
            tmp = cat(tmp, "haha");
            [color=blue][color=green][color=darkred]
            > >> printf("%s\n",t mp);
            > >> return 0;
            > >>}[/color]
            > >
            > >
            > > char *cat(char *s, const char *t)
            > > {
            > > char *tmp;
            > >
            > > if (s == NULL) {
            > > tmp = realloc(s, strlen(t) + 1);
            > > *tmp = '\0';[/color]
            >
            > Here is a slight flaw. You do not want to assign to *tmp
            > BEFORE you have checked tmp value for a possible NULL.[/color]

            Thank you.
            [color=blue][color=green]
            > > if (tmp == NULL) {
            > > fputs("realloc error",stderr);
            > > exit(EXIT_FAILU RE);
            > > }[/color][/color]
            [color=blue]
            > This definition will abort the program if dynamic allocation
            > fails. Another less virulent act on the executable would be
            > give a signal to the caller, via the return value, or some
            > other means, that the function was not successful in the
            > operation.[/color]

            /* I'm taking another shot. */

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

            char *cat(char *s, const char *t);

            int main(void)
            {
            char *s;

            s = cat(NULL, "oh");
            if (s != NULL) {
            printf("%s\n", s);
            }
            s = cat(s, "haha");
            if (s != NULL) {
            printf("%s\n", s);
            }
            return 0;
            }

            char *cat(char *s, const char *t)
            {
            char *tmp;

            if (s == NULL) {
            tmp = calloc(1, strlen(t) + 1);
            } else {
            tmp = realloc(s, strlen(s) + strlen(t) + 1);
            }
            s = tmp;
            if (tmp != NULL) {
            while (*tmp++ != '\0') {
            ;
            }
            tmp--;
            while ((*tmp++ = *t++) != '\0') {
            ;
            }
            }
            return s;
            }

            --
            pete

            Comment

            • pete

              #7
              Re: realloc

              pete wrote:[color=blue]
              >
              > Al Bowers wrote:[color=green]
              > >
              > > pete wrote:[/color][/color]

              [color=blue]
              > /* I'm taking another shot. */
              >
              > #include <stdio.h>
              > #include <string.h>
              > #include <stdlib.h>
              >
              > char *cat(char *s, const char *t);
              >
              > int main(void)
              > {
              > char *s;
              >
              > s = cat(NULL, "oh");
              > if (s != NULL) {
              > printf("%s\n", s);
              > }
              > s = cat(s, "haha");
              > if (s != NULL) {
              > printf("%s\n", s);
              > }
              > return 0;
              > }[/color]

              /*
              ** I got it into my head that writing cat using strcat,
              ** might be cheating in some way, but ...
              */

              char *cat(char *s, const char *t)
              {
              char *tmp;

              if (s == NULL) {
              tmp = calloc(1, strlen(t) + 1);
              } else {
              tmp = realloc(s, strlen(s) + strlen(t) + 1);
              }
              s = tmp;
              if (s != NULL) {
              strcat(s, t);
              }
              return s;
              }

              --
              pete

              Comment

              • Flash Gordon

                #8
                Re: realloc

                Roy wrote:[color=blue]
                > Hi all :
                > My code below :
                >
                > #include <stdio.h>
                > #include <string.h>
                > #include <stdlib.h>
                >
                > char *cat(char *s, const char *t)
                > {
                > char *tmp;
                >
                > if (s == NULL)
                > tmp = realloc(s,strle n(t) + 1);[/color]

                At this point, the memory pointed to by tmp will be in a random state.
                Also you might as well use malloc since you know s is NULL.
                [color=blue]
                > else
                > tmp = realloc(s,strle n(s) + strlen(t) + 1);
                > if (tmp == NULL) {
                > fputs("realloc error",stderr);
                > exit(1);
                > }
                > while (*tmp++); // search for '\0' and stop[/color]

                So the while loop above could do anything if s is NULL, as it is on your
                first call.

                Also, you have just had the string searched for a '\0' by calling
                strlen, so scanning it again seems a bit silly to me.

                Also, please don't use // comments on Usenet. They do not survive line
                wrapping and, for your information, were not standard in C89 although
                they are a common extension. They are standard in C99, but most
                implementations that I am aware of are not conforming C99 implementations .
                [color=blue]
                > tmp--; //the position of '\0'
                > while (*tmp++ = *t++) ;[/color]

                Again, you know the length and could use memcpy which *may* be more
                efficient.
                [color=blue]
                > s = tmp;
                > return s;
                > }
                >
                > int main()
                > {
                > char *tmp;
                > char *s;
                >
                > s = NULL;
                > tmp = cat(s,"oh");
                > printf("%s\n",t mp);
                > tmp = cat(s,"haha");[/color]

                Here you are using s, but s will still be NULL, and loosing the pointer
                you obtained by the previous call to cat.
                [color=blue]
                > printf("%s\n",t mp);
                > return 0;
                > }
                > my problem is when I run the program the reslut is some blanks. I just
                > wrote a small routine like strcat and check the realloc and tmp pointer
                > carefully but found nothing.[/color]

                Try the following modified version...

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

                char *cat(char *s, const char *t)
                {
                char *tmp;
                size_t slen;
                size_t tlen = strlen(t);

                if (s == NULL)
                slen = 0;
                else
                slen = strlen(s);
                tmp = realloc(s,slen + tlen + 1);

                if (tmp == NULL) {
                free(s); /* improve chances of the fputs not running out of memory */
                fputs("realloc error",stderr);
                exit(1);
                }
                memcpy(tmp+slen ,t,tlen);
                tmp[slen+tlen]='\0';
                return tmp;
                }

                int main(void)
                {
                char *tmp;
                char *s = NULL;
                tmp = cat(s,"oh");
                printf("%s\n",t mp);
                s = cat(tmp,"haha") ;
                printf("%s\n",s );
                return 0;
                }
                --
                Flash Gordon
                Living in interesting times.
                Although my email address says spam, it is real and I read it.

                Comment

                • Michael Knaup

                  #9
                  Re: realloc

                  pete wrote:
                  [color=blue]
                  > pete wrote:[color=green]
                  >>
                  >> Al Bowers wrote:[color=darkred]
                  >> >
                  >> > pete wrote:[/color][/color]
                  >
                  >[color=green]
                  >> /* I'm taking another shot. */
                  >>
                  >> #include <stdio.h>
                  >> #include <string.h>
                  >> #include <stdlib.h>
                  >>
                  >> char *cat(char *s, const char *t);
                  >>
                  >> int main(void)
                  >> {
                  >> char *s;
                  >>
                  >> s = cat(NULL, "oh");
                  >> if (s != NULL) {
                  >> printf("%s\n", s);
                  >> }
                  >> s = cat(s, "haha");
                  >> if (s != NULL) {
                  >> printf("%s\n", s);
                  >> }
                  >> return 0;
                  >> }[/color]
                  >
                  > /*
                  > ** I got it into my head that writing cat using strcat,
                  > ** might be cheating in some way, but ...
                  > */
                  >
                  > char *cat(char *s, const char *t)
                  > {
                  > char *tmp;
                  >
                  > if (s == NULL) {
                  > tmp = calloc(1, strlen(t) + 1);
                  > } else {
                  > tmp = realloc(s, strlen(s) + strlen(t) + 1);
                  > }
                  > s = tmp;[/color]

                  Why reusing s here, s is only a copy of your s in the main function. So if
                  you want to change your "main" s you have to use a pointer to a pointer
                  like All did.
                  [color=blue]
                  > if (s != NULL) {
                  > strcat(s, t);[/color]

                  Maybe the use of malloc/realoc and memcpy is a "little" bit faster than your
                  solution
                  [color=blue]
                  > }
                  > return s;
                  > }
                  >[/color]

                  This one should work

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

                  #if __STDC_VERSION_ _ < 199901L
                  # define restrict
                  #endif

                  char* concat (char **s, restrict const char *t)
                  {
                  char *tmp = NULL;

                  if (NULL == t) {
                  if (NULL != s) {
                  tmp = *s;
                  }
                  } else {
                  size_t length = 1u + strlen(t);

                  if (NULL == s || NULL == *s) {
                  tmp = malloc (length);
                  if (NULL != tmp) {
                  memcpy(tmp, t, length);
                  if (NULL != s) {
                  *s = tmp;
                  }
                  }
                  } else {
                  size_t old_len = strlen(*s);
                  tmp = realloc (*s, old_len + length);
                  if (NULL != tmp) {
                  memcpy (tmp + old_len, t, length);
                  *s = tmp;
                  }
                  }
                  }
                  return tmp;
                  }

                  int main(void)
                  {
                  char *string = NULL;
                  char *tmp;

                  concat(&string, "oh");
                  printf ("*%p == %s\n", string, string);
                  tmp = string;
                  do {
                  concat(&string, "haha");
                  } while (tmp == string);
                  printf ("*%p (%zd) == %s\n", string, strlen(string), string);

                  free (string);

                  return EXIT_SUCCESS;
                  }
                  --
                  Michael Knaup

                  Comment

                  • Michael Knaup

                    #10
                    Re: realloc

                    And true you should check against NULL after calling concat[color=blue]
                    > int main(void)
                    > {
                    > char *string = NULL;
                    > char *tmp;
                    >
                    > concat(&string, "oh");[/color]

                    if (NULL == string)
                    return EXIT_FAILURE;
                    [color=blue]
                    > printf ("*%p == %s\n", string, string);
                    > tmp = string;
                    > do {[/color]
                    if (NULL == concat(&string, "haha"))
                    break;
                    [color=blue]
                    > } while (tmp == string);
                    > printf ("*%p (%zd) == %s\n", string, strlen(string), string);
                    >
                    > free (string);
                    >
                    > return EXIT_SUCCESS;
                    > }[/color]

                    --
                    Michael Knaup

                    Comment

                    • pete

                      #11
                      Re: realloc

                      Michael Knaup wrote:[color=blue]
                      >
                      > pete wrote:
                      >[color=green]
                      > > pete wrote:[color=darkred]
                      > >>
                      > >> Al Bowers wrote:
                      > >> >
                      > >> > pete wrote:[/color][/color][/color]
                      [color=blue][color=green][color=darkred]
                      > >> s = cat(s, "haha");
                      > >> if (s != NULL) {[/color][/color][/color]
                      [color=blue][color=green]
                      > > char *cat(char *s, const char *t)
                      > > {
                      > > char *tmp;
                      > >
                      > > if (s == NULL) {
                      > > tmp = calloc(1, strlen(t) + 1);
                      > > } else {
                      > > tmp = realloc(s, strlen(s) + strlen(t) + 1);
                      > > }
                      > > s = tmp;[/color]
                      >
                      > Why reusing s here,
                      > s is only a copy of your s in the main function. So if
                      > you want to change your "main"
                      > s you have to use a pointer to a pointer
                      > like All did.[/color]

                      I changed the value of s in main this way:
                      s = cat(NULL, "oh");
                      s = cat(s, "haha");
                      I wanted to return either a pointer to memory,
                      or return NULL, from cat.
                      [color=blue]
                      > free (string);[/color]

                      I like that line of code.

                      My personal preference is to call free from the same
                      function that makes the call to *alloc.,
                      except for lists.

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

                      int main(void)
                      {
                      char *s;
                      char *old_s;

                      old_s = s = malloc(sizeof "oh");
                      if (s != NULL) {
                      puts(strcpy(s, "oh"));
                      }
                      if (s != NULL) {
                      s = realloc(s, strlen("oh") + sizeof "haha");
                      } else {
                      s = calloc(1, sizeof "haha");
                      }
                      if (s != NULL) {
                      puts(strcat(s, "haha"));
                      } else {
                      s = old_s;
                      }
                      free(s);
                      return 0;
                      }

                      --
                      pete

                      Comment

                      • Michael Knaup

                        #12
                        Re: realloc

                        pete wrote:
                        [color=blue]
                        > Michael Knaup wrote:[color=green]
                        >>
                        >> pete wrote:
                        >>[color=darkred]
                        >> > pete wrote:
                        >> >>
                        >> >> Al Bowers wrote:
                        >> >> >
                        >> >> > pete wrote:[/color][/color]
                        >[color=green][color=darkred]
                        >> >> s = cat(s, "haha");
                        >> >> if (s != NULL) {[/color][/color]
                        >[color=green][color=darkred]
                        >> > char *cat(char *s, const char *t)
                        >> > {
                        >> > char *tmp;
                        >> >
                        >> > if (s == NULL) {
                        >> > tmp = calloc(1, strlen(t) + 1);
                        >> > } else {
                        >> > tmp = realloc(s, strlen(s) + strlen(t) + 1);
                        >> > }
                        >> > s = tmp;[/color]
                        >>
                        >> Why reusing s here,
                        >> s is only a copy of your s in the main function. So if
                        >> you want to change your "main"
                        >> s you have to use a pointer to a pointer
                        >> like All did.[/color]
                        >
                        > I changed the value of s in main this way:
                        > s = cat(NULL, "oh");
                        > s = cat(s, "haha");[/color]

                        But what happens when the memory allocation in cat fails?
                        I think we have a memory leckage then, don't we?
                        [color=blue]
                        > I wanted to return either a pointer to memory,
                        > or return NULL, from cat.
                        >[color=green]
                        >> free (string);[/color]
                        >
                        > I like that line of code.
                        >
                        > My personal preference is to call free from the same
                        > function that makes the call to *alloc.,
                        > except for lists.[/color]

                        Yes thats always a good idea, and you are free to do so with both solutions.
                        And yes you can do the whole task in place, but do you wanna mantain this
                        code?
                        --
                        Michael Knaup

                        Comment

                        • pete

                          #13
                          Re: realloc

                          Michael Knaup wrote:[color=blue]
                          > pete wrote:[color=green]
                          > > Michael Knaup wrote:[color=darkred]
                          > >> pete wrote:
                          > >> > pete wrote:
                          > >> >> Al Bowers wrote:
                          > >> >> > pete wrote:[/color][/color][/color]
                          [color=blue][color=green][color=darkred]
                          > >> >> s = cat(s, "haha");
                          > >> >> if (s != NULL) {[/color]
                          > >[color=darkred]
                          > >> > char *cat(char *s, const char *t)
                          > >> > {
                          > >> > char *tmp;
                          > >> >
                          > >> > if (s == NULL) {
                          > >> > tmp = calloc(1, strlen(t) + 1);
                          > >> > } else {
                          > >> > tmp = realloc(s, strlen(s) + strlen(t) + 1);
                          > >> > }
                          > >> > s = tmp;[/color][/color][/color]
                          [color=blue][color=green]
                          > > I changed the value of s in main this way:
                          > > s = cat(NULL, "oh");
                          > > s = cat(s, "haha");[/color][/color]
                          [color=blue]
                          > But what happens when the memory allocation in cat fails?
                          > I think we have a memory leckage then, don't we?[/color]

                          Yes we do.
                          I should have used an old_s variable there too.
                          Especially since the subject is realloc,
                          and saving the value of the old pointer is very much
                          a part of using realloc correctly.
                          [color=blue][color=green]
                          > > My personal preference is to call free from the same
                          > > function that makes the call to *alloc.,
                          > > except for lists.[/color]
                          >
                          > Yes thats always a good idea,
                          > and you are free to do so with both solutions.
                          > And yes you can do the whole task in place,
                          > but do you wanna mantain this code?[/color]

                          I don't know.
                          I can't really see this code
                          being anything other than a realloc exercise.

                          Comment

                          • pete

                            #14
                            Re: realloc

                            Flash Gordon wrote:
                            [color=blue]
                            > /* improve chances of the fputs not running out of memory */[/color]

                            #include <stdio.h>

                            int fput_s(const char *s, FILE *stream);

                            int main(void)
                            {
                            fput_s(
                            "What kind of memory usage "
                            "are you envisioning for fputs?\n",
                            stdout
                            );
                            return 0;
                            }

                            int fput_s(const char *s, FILE *stream)
                            {
                            while (*s != '\0') {
                            if (putc(*s, stream) == EOF) {
                            return EOF;
                            }
                            ++s;
                            }
                            return 0;
                            }

                            Comment

                            • Al Bowers

                              #15
                              Re: realloc



                              Michael Knaup wrote:

                              [color=blue]
                              >
                              > Maybe the use of malloc/realoc and memcpy is a "little" bit faster than your
                              > solution
                              >[/color]

                              The use of memcpy is certainly a good option. The use of the
                              malloc/realloc combination is not neccessary. Just use
                              function realloc to do the allocations(see below).
                              I believe that was the intent of the OP.

                              Function cat below, is an attempt rewrite of this function
                              concat. I believe all the operations are safe.
                              [color=blue]
                              > char* concat (char **s, restrict const char *t)
                              > {
                              > char *tmp = NULL;
                              >
                              > if (NULL == t) {
                              > if (NULL != s) {
                              > tmp = *s;
                              > }
                              > } else {
                              > size_t length = 1u + strlen(t);
                              >
                              > if (NULL == s || NULL == *s) {
                              > tmp = malloc (length);
                              > if (NULL != tmp) {
                              > memcpy(tmp, t, length);
                              > if (NULL != s) {
                              > *s = tmp;
                              > }
                              > }
                              > } else {
                              > size_t old_len = strlen(*s);
                              > tmp = realloc (*s, old_len + length);
                              > if (NULL != tmp) {
                              > memcpy (tmp + old_len, t, length);
                              > *s = tmp;
                              > }
                              > }
                              > }
                              > return tmp;
                              > }
                              >[/color]

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

                              char *cat(char **s1, const char *s2);

                              int main(void)
                              {
                              char *s = NULL;

                              if(cat(&s,"Hell o"))
                              {
                              if(cat(&s," and Goodbye"))
                              printf("s = \"%s\"\n",s) ;
                              else puts("Memory allocation failure");
                              }
                              else puts("Memory allocation failure");
                              free(s);
                              s = cat(NULL,"This string generated with NULL 1st argument");
                              if(s) printf("s = \"%s\"\n",s) ;
                              free(s);
                              return 0;
                              }

                              char *cat(char **s1, const char *s2)
                              {
                              char *ret = NULL;

                              if(s2)
                              {
                              char *tmp;
                              size_t sz1 = (!s1||!*s1)?0:s trlen(*s1);
                              size_t sz2 = strlen(s2);
                              if((tmp = realloc(s1?*s1: NULL,sz1+sz2+1) ) != NULL)
                              {
                              memcpy(tmp+sz1, s2,sz2+1);
                              ret = (s1)?*s1 = tmp:tmp;
                              }
                              }
                              return ret;
                              }

                              --
                              Al Bowers
                              Tampa, Fl USA
                              mailto: xabowers@myrapi dsys.com (remove the x to send email)
                              Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


                              Comment

                              Working...