How to realize the function ?

Discussion in 'C Programming' started by yezi, Dec 1, 2005.

  1. yezi

    yezi Guest

    Hi: all:

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

    Thanks
     
    yezi, Dec 1, 2005
    #1
    1. Advertising

  2. yezi

    Skarmander Guest

    yezi wrote:
    > Hi: all:
    >
    > is some mem cat function with is like "strcat" ?
    >

    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.
     
    Skarmander, Dec 1, 2005
    #2
    1. Advertising

  3. yezi

    Richard Bos Guest

    "yezi" <> wrote:

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


    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
     
    Richard Bos, Dec 1, 2005
    #3
  4. "yezi" <> writes:

    > Hi: all:
    >
    > is some mem cat function with is like "strcat" ?


    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
     
    Niklas Norrthon, Dec 1, 2005
    #4
  5. yezi

    Suman Guest

    Richard Bos wrote:
    > "yezi" <> wrote:
    >
    > > is some mem cat function with is like "strcat" ?

    >
    > 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.)


    Then, we shouldn't have memcmp either ? ;)
     
    Suman, Dec 1, 2005
    #5
  6. yezi

    Richard Bos Guest

    "Suman" <> wrote:

    > Richard Bos wrote:
    > > "yezi" <> wrote:
    > >
    > > > is some mem cat function with is like "strcat" ?

    > >
    > > 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.)

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


    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
     
    Richard Bos, Dec 1, 2005
    #6
  7. yezi

    Suman Guest

    Richard Bos wrote:
    > "Suman" <> wrote:
    >
    > > Richard Bos wrote:


    [ replying to the OP]

    > > > 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 ? ;)

    >
    > 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:


    Correct. To me, the strcat/memcat comparison was a poor one.

    > void *memcat(void *dest, void *src, size_t at, size_t len)
    > {


    I felt free to drop a:
    return

    > memcpy(dest+at, src, len);
    > }
     
    Suman, Dec 1, 2005
    #7
  8. yezi

    Richard Bos Guest

    "Suman" <> wrote:

    > Richard Bos wrote:
    > > "Suman" <> wrote:
    > >
    > > > Richard Bos wrote:

    >
    > [ replying to the OP]
    >
    > > > > 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 ? ;)

    > >
    > > 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:

    >
    > Correct. To me, the strcat/memcat comparison was a poor one.


    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.

    > > void *memcat(void *dest, void *src, size_t at, size_t len)
    > > {

    >
    > I felt free to drop a:
    > return


    Er... yes... sorry.

    > > memcpy(dest+at, src, len);
    > > }



    Richard
     
    Richard Bos, Dec 1, 2005
    #8
  9. yezi

    Rudolf Guest

    In article <4all.nl>,
    (Richard Bos) wrote:

    > "yezi" <> wrote:
    >
    > > is some mem cat function with is like "strcat" ?

    >
    > 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.)


    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?
     
    Rudolf, Dec 2, 2005
    #9
  10. Rudolf <> writes:
    > In article <4all.nl>,
    > (Richard Bos) wrote:
    >
    >> "yezi" <> wrote:
    >>
    >> > is some mem cat function with is like "strcat" ?

    >>
    >> 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.)

    >
    > 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?


    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_Keith) <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.
     
    Keith Thompson, Dec 2, 2005
    #10
  11. Keith Thompson said:

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


    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)+(dstlen), 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
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 2, 2005
    #11
  12. Rudolf <> writes:

    > In article <4all.nl>,
    > (Richard Bos) wrote:
    >
    > > "yezi" <> wrote:
    > >
    > > > is some mem cat function with is like "strcat" ?

    > >
    > > 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.)

    >
    > 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?


    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_buffer[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;
    automatic_buffer = i;
    free_store_ptr = i;
    }

    static_ptr = static_buffer + BUF_SZ / 2;
    automatic_ptr = automatic_buffer + 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_ptr, automatic_buffer);
    memcat(automatic_ptr, static_ptr);
    memcat(free_store_ptr, automatic_ptr);

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

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 2, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lei Guangfu
    Replies:
    1
    Views:
    312
    Natty Gur
    Oct 19, 2003
  2. Shugong Wang
    Replies:
    2
    Views:
    9,623
    Raymond Martineau
    Nov 28, 2004
  3. tuchfly
    Replies:
    5
    Views:
    1,052
    Beej Jorgensen
    Dec 10, 2009
  4. Lee Tow

    Can it realize these function using asp?

    Lee Tow, Aug 10, 2005, in forum: ASP General
    Replies:
    2
    Views:
    127
    Aaron Bertrand [SQL Server MVP]
    Aug 10, 2005
  5. Shiwei Zhang
    Replies:
    7
    Views:
    321
Loading...

Share This Page