How to realize the function ?

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

    How to realize the function ?

    Hi: all:

    is some mem cat function with is like "strcat" ?

    Thanks

  • Skarmander

    #2
    Re: How to realize the function ?

    yezi wrote:[color=blue]
    > Hi: all:
    >
    > is some mem cat function with is like "strcat" ?
    >[/color]
    No. You can realloc() one memory region, then memcpy() the other at the end;
    or just malloc() a new one and copy both. You must know the size of both
    regions, obviously.

    S.

    Comment

    • Richard Bos

      #3
      Re: How to realize the function ?

      "yezi" <ye_line@hotmai l.com> wrote:
      [color=blue]
      > is some mem cat function with is like "strcat" ?[/color]

      No, and there cannot be. Consider: how does strcat() know where the
      first string ends? How would a hypothetical memcat() do the same? (Hint:
      it cannot.)

      Richard

      Comment

      • Suman

        #4
        Re: How to realize the function ?


        Richard Bos wrote:[color=blue]
        > "yezi" <ye_line@hotmai l.com> wrote:
        >[color=green]
        > > is some mem cat function with is like "strcat" ?[/color]
        >
        > No, and there cannot be. Consider: how does strcat() know where the
        > first string ends? How would a hypothetical memcat() do the same? (Hint:
        > it cannot.)[/color]

        Then, we shouldn't have memcmp either ? ;)

        Comment

        • Richard Bos

          #5
          Re: How to realize the function ?

          "Suman" <skarpio@gmail. com> wrote:
          [color=blue]
          > Richard Bos wrote:[color=green]
          > > "yezi" <ye_line@hotmai l.com> wrote:
          > >[color=darkred]
          > > > is some mem cat function with is like "strcat" ?[/color]
          > >
          > > No, and there cannot be. Consider: how does strcat() know where the
          > > first string ends? How would a hypothetical memcat() do the same? (Hint:
          > > it cannot.)[/color]
          >
          > Then, we shouldn't have memcmp either ? ;)[/color]

          memcmp() doesn't have to figure out for itself where its data ends. You
          tell it. You _could_ write a memcat() that you also tell yourself where
          its first data block ends. It would be rather pointless, though:

          void *memcat(void *dest, void *src, size_t at, size_t len)
          {
          memcpy(dest+at, src, len);
          }

          Richard

          Comment

          • Suman

            #6
            Re: How to realize the function ?


            Richard Bos wrote:[color=blue]
            > "Suman" <skarpio@gmail. com> wrote:
            >[color=green]
            > > Richard Bos wrote:[/color][/color]

            [ replying to the OP]
            [color=blue][color=green][color=darkred]
            > > > Consider: how does strcat() know where the
            > > > first string ends? How would a hypothetical memcat() do the same? (Hint:
            > > > it cannot.)[/color]
            > >
            > > Then, we shouldn't have memcmp either ? ;)[/color]
            >
            > memcmp() doesn't have to figure out for itself where its data ends. You
            > tell it. You _could_ write a memcat() that you also tell yourself where
            > its first data block ends. It would be rather pointless, though:[/color]

            Correct. To me, the strcat/memcat comparison was a poor one.
            [color=blue]
            > void *memcat(void *dest, void *src, size_t at, size_t len)
            > {[/color]

            I felt free to drop a:
            return
            [color=blue]
            > memcpy(dest+at, src, len);
            > }[/color]

            Comment

            • Niklas Norrthon

              #7
              Re: How to realize the function ?

              "yezi" <ye_line@hotmai l.com> writes:
              [color=blue]
              > Hi: all:
              >
              > is some mem cat function with is like "strcat" ?[/color]

              Not in the sense that it appends one region of memory to
              another, since there is no way to know where a memory
              region ends. But if you know how large your regions are
              you could just use memcpy or memmove from <string.h>

              If is quite trivial to implement strcat in terms of strlen
              to get the sizes, and memcpy to append. I don't know what
              you want to accomplish, but you could take this code as
              a starting point:

              (Original strcat is probably more efficient, since it doesn't
              have to iterate through the src string twice).

              #include <string.h>

              char*
              my_strcat(char* dest, char* src)
              {
              size_t sz_d = strlen(dest);
              size_t sz_s = strlen(src) + 1; /* including '\0' */

              memcpy(dest + sz_d, src, sz_s);
              return dest;
              }

              /Niklas Norrthon

              Comment

              • Richard Bos

                #8
                Re: How to realize the function ?

                "Suman" <skarpio@gmail. com> wrote:
                [color=blue]
                > Richard Bos wrote:[color=green]
                > > "Suman" <skarpio@gmail. com> wrote:
                > >[color=darkred]
                > > > Richard Bos wrote:[/color][/color]
                >
                > [ replying to the OP]
                >[color=green][color=darkred]
                > > > > Consider: how does strcat() know where the
                > > > > first string ends? How would a hypothetical memcat() do the same? (Hint:
                > > > > it cannot.)
                > > >
                > > > Then, we shouldn't have memcmp either ? ;)[/color]
                > >
                > > memcmp() doesn't have to figure out for itself where its data ends. You
                > > tell it. You _could_ write a memcat() that you also tell yourself where
                > > its first data block ends. It would be rather pointless, though:[/color]
                >
                > Correct. To me, the strcat/memcat comparison was a poor one.[/color]

                I agree, but that's what the OP wanted: a function that does for the
                mem*() functions what strcat() does for str*(). Since that comparison
                does indeed not work, the answer is: there isn't one.
                [color=blue][color=green]
                > > void *memcat(void *dest, void *src, size_t at, size_t len)
                > > {[/color]
                >
                > I felt free to drop a:
                > return[/color]

                Er... yes... sorry.
                [color=blue][color=green]
                > > memcpy(dest+at, src, len);
                > > }[/color][/color]


                Richard

                Comment

                • Rudolf

                  #9
                  Re: How to realize the function ?

                  In article <438eac39.14500 2778@news.xs4al l.nl>,
                  rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                  [color=blue]
                  > "yezi" <ye_line@hotmai l.com> wrote:
                  >[color=green]
                  > > is some mem cat function with is like "strcat" ?[/color]
                  >
                  > No, and there cannot be. Consider: how does strcat() know where the
                  > first string ends? How would a hypothetical memcat() do the same? (Hint:
                  > it cannot.)[/color]

                  Since functions like free() and realloc() know how big a memory region
                  is, then why can't the hypothetical memcat() know how big it is?

                  Comment

                  • Keith Thompson

                    #10
                    Re: How to realize the function ?

                    Rudolf <rthered@bigfoo t.com> writes:[color=blue]
                    > In article <438eac39.14500 2778@news.xs4al l.nl>,
                    > rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                    >[color=green]
                    >> "yezi" <ye_line@hotmai l.com> wrote:
                    >>[color=darkred]
                    >> > is some mem cat function with is like "strcat" ?[/color]
                    >>
                    >> No, and there cannot be. Consider: how does strcat() know where the
                    >> first string ends? How would a hypothetical memcat() do the same? (Hint:
                    >> it cannot.)[/color]
                    >
                    > Since functions like free() and realloc() know how big a memory region
                    > is, then why can't the hypothetical memcat() know how big it is?[/color]

                    Not really. The existing mem* functions work on arbitrary blocks of
                    memory, which could be allocated in any of several ways (stack, heap,
                    global). Whatever hidden information exists for malloc()ed objects
                    doesn't apply to the others.

                    If the hypothetical memcat() applied only to malloc()ed objects, it
                    could work, but then it probably shouldn't have name starting with
                    "mem".

                    I haven't seen a rigorous description in this thread of just what
                    memcat() is supposed to do.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • Richard Heathfield

                      #11
                      Re: How to realize the function ?

                      Keith Thompson said:
                      [color=blue]
                      > I haven't seen a rigorous description in this thread of just what
                      > memcat() is supposed to do.[/color]

                      Presumably it would copy an arbitrary amount of memory to the end of another
                      arbitrary amount of memory.

                      #define mem_cat(dst, dstlen, src, srclen) \
                      memcpy((dst)+(d stlen), src, srclen)

                      I'm still trying to work out what all the fuss is about.

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at above domain (but drop the www, obviously)

                      Comment

                      • Niklas Norrthon

                        #12
                        Re: How to realize the function ?

                        Rudolf <rthered@bigfoo t.com> writes:
                        [color=blue]
                        > In article <438eac39.14500 2778@news.xs4al l.nl>,
                        > rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                        >[color=green]
                        > > "yezi" <ye_line@hotmai l.com> wrote:
                        > >[color=darkred]
                        > > > is some mem cat function with is like "strcat" ?[/color]
                        > >
                        > > No, and there cannot be. Consider: how does strcat() know where the
                        > > first string ends? How would a hypothetical memcat() do the same? (Hint:
                        > > it cannot.)[/color]
                        >
                        > Since functions like free() and realloc() know how big a memory region
                        > is, then why can't the hypothetical memcat() know how big it is?[/color]

                        What would be the semanics of the following program with your
                        suggested memcat?

                        #inlcude <stdlib.h>
                        #include <memcat_header. h> /* whatever that is */

                        #define BUF_SZ 100
                        int static_buffer[BUF_SZ];
                        int* static_ptr;

                        int main(void)
                        {
                        int automatic_buffe r[BUF_SZ];
                        int* automatic_ptr;
                        int* free_store_ptr = malloc(BUF_SZ * sizeof *free_store_ptr );
                        /* check for malloc failure */

                        for (i = 0; i < sizeof static_buffer; ++i) {
                        static_buffer[i] = i;
                        automatic_buffe r[i] = i;
                        free_store_ptr[i] = i;
                        }

                        static_ptr = static_buffer + BUF_SZ / 2;
                        automatic_ptr = automatic_buffe r + BUF_SZ / 2;

                        /*
                        * In the following calls to memcat:
                        * how much memory would be copied,
                        * and where would the destiantion be?
                        * Explain that, and I'll tell you why
                        * memcat as your proposal can't exist
                        */

                        memcat(static_p tr, automatic_buffe r);
                        memcat(automati c_ptr, static_ptr);
                        memcat(free_sto re_ptr, automatic_ptr);

                        /* and so on, with every possible combination... */
                        return EXIT_FAILURE;
                        }

                        /Niklas Norrthon

                        Comment

                        Working...