RE: Need help with memcpy_rom funtion

Discussion in 'C Programming' started by Rudolf.Erasmus@za.saabgroup.com, Feb 5, 2008.

  1. Guest

    Hi to all,
    I'm new to comp.lang.c and have a question regarding a function that
    I'm testing: void * memcpy_rom ( void *s1, const void *s2, size_t n)
    /*
    Description
    Copy n bytes of memory from s2 to s1.
    Destination: s1
    Source: s2
    size in bytes: n
    */

    My testing program that I am using allows me to set up the values to
    the input parameters, which I do.
    The input parameters that I for s2 is: abcde
    The input parameter that I enter for s1 is: xxx
    The input parameter that I enter for n is: 4

    According to me what it should do is to simply copy 4 bytes, thus
    "abcd" from s2 to s1. s1 should now have an expected value of abcd and
    the function should return a pointer to s1.

    This is not what happens.
    It gives me a error and tells me that it expects s1 to be "bcde"

    Question: Is there something about this function I'm not aware of?
     
    , Feb 5, 2008
    #1
    1. Advertising

  2. Rudolf:

    > I'm new to comp.lang.c and have a question regarding a function that
    > I'm testing: void * memcpy_rom ( void *s1, const void *s2, size_t n)
    > /*
    > Description
    > Copy n bytes of memory from s2 to s1.
    > Destination: s1
    > Source: s2
    > size in bytes: n
    > */



    I'd suggest you use descriptive names, like dest and src.


    > My testing program that I am using allows me to set up the values to
    > the input parameters, which I do.
    > The input parameters that I for s2 is: abcde
    > The input parameter that I enter for s1 is: xxx
    > The input parameter that I enter for n is: 4
    >
    > According to me what it should do is to simply copy 4 bytes, thus
    > "abcd" from s2 to s1. s1 should now have an expected value of abcd and
    > the function should return a pointer to s1.
    >
    > This is not what happens.
    > It gives me a error and tells me that it expects s1 to be "bcde"
    >
    > Question: Is there something about this function I'm not aware of?



    You didn't post the code.

    The standard library has a function called "memcpy" that does exactly
    what you're trying to do. It could be implemented as so:

    #include <stddef.h> /* size_t */

    void *memcpy(void *const dest,void const *const src,size_t len)
    {
    char unsigned *pdest = dest, *psrc = src;

    while (len--) *pdest++ = *psrc++;

    return dest;
    }

    char const hello[] = "Hello";

    int main(void)
    {
    char buf[12];

    memcpy(buf,hello,sizeof hello);

    return 0;
    }



    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Feb 5, 2008
    #2
    1. Advertising

  3. Guest

    On Feb 5, 11:14 am, "Tomás Ó hÉilidhe" <> wrote:
    > Rudolf:
    >
    > > I'm new to comp.lang.c and have a question regarding a function that
    > > I'm testing: void * memcpy_rom ( void *s1, const void *s2, size_t n)
    > > /*
    > >   Description
    > >     Copy n bytes of memory from s2 to s1.
    > >       Destination:   s1
    > >       Source:         s2
    > >       size in bytes: n
    > > */

    >
    > I'd suggest you use descriptive names, like dest and src.
    >
    > > My testing program that I am using allows me to set up the values to
    > > the input parameters, which I do.
    > > The input parameters that I for s2 is: abcde
    > > The input parameter that I enter for s1 is: xxx
    > > The input parameter that I enter for n is: 4

    >
    > > According to me what it should do is to simply copy 4 bytes, thus
    > > "abcd" from s2 to s1. s1 should now have an expected value of abcd and
    > > the function should return a pointer to s1.

    >
    > > This is not what happens.
    > > It gives me a error and tells me that it expects s1 to be "bcde"

    >
    > > Question: Is there something about this function I'm not aware of?

    >
    > You didn't post the code.
    >
    > The standard library has a function called "memcpy" that does exactly
    > what you're trying to do. It could be implemented as so:
    >
    > #include <stddef.h>    /* size_t */
    >
    > void *memcpy(void *const dest,void const *const src,size_t len)
    > {
    >     char unsigned *pdest = dest, *psrc = src;
    >
    >     while (len--) *pdest++ = *psrc++;
    >
    >     return dest;
    >
    > }
    >
    > char const hello[] = "Hello";
    >
    > int main(void)
    > {
    >     char buf[12];
    >
    >     memcpy(buf,hello,sizeof hello);
    >
    >     return 0;
    >
    > }
    >
    > --
    > Tomás Ó hÉilidhe


    Sorry for not posting the code.
    I have found my mistake.
    I was setting the alignment of the source: s2 to +1 which resulted in
    it copying from the 2nd byte onwards. Thanks anyways for your help.
     
    , Feb 5, 2008
    #3
  4. pete Guest

    Tomás Ó hÉilidhe wrote:
    >
    > Rudolf:
    >
    > > I'm new to comp.lang.c and have a question regarding a function that
    > > I'm testing: void * memcpy_rom ( void *s1, const void *s2, size_t n)
    > > /*
    > > Description
    > > Copy n bytes of memory from s2 to s1.
    > > Destination: s1
    > > Source: s2
    > > size in bytes: n
    > > */

    >
    > I'd suggest you use descriptive names, like dest and src.


    OP is mimicking the C89 Standard Synopsis of memcpy.

    That's the format that I also use
    for toy versions of standard library functions.

    --
    pete
     
    pete, Feb 5, 2008
    #4
  5. CBFalconer Guest

    wrote:
    >
    > I'm new to comp.lang.c and have a question regarding a function that
    > I'm testing: void * memcpy_rom ( void *s1, const void *s2, size_t n)
    > /*
    > Description
    > Copy n bytes of memory from s2 to s1.
    > Destination: s1
    > Source: s2
    > size in bytes: n
    > */
    >
    > My testing program that I am using allows me to set up the values to


    Why? Why not simply use the memcpy() system function, whose
    prototype is in <string.h>. Hopefully the result is optimized for
    your system.


    7.21.2.1 The memcpy function

    Synopsis
    [#1]
    #include <string.h>
    void *memcpy(void * restrict s1,
    const void * restrict s2,
    size_t n);

    Description

    [#2] The memcpy function copies n characters from the object
    pointed to by s2 into the object pointed to by s1. If
    copying takes place between objects that overlap, the
    behavior is undefined.

    Returns

    [#3] The memcpy function returns the value of s1.


    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 5, 2008
    #5
    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. Sam Holden

    Re: fraction reducing funtion

    Sam Holden, Aug 3, 2003, in forum: C++
    Replies:
    2
    Views:
    372
    Sam Holden
    Aug 4, 2003
  2. garyolsen

    Virtual Funtion Questions

    garyolsen, Dec 2, 2003, in forum: C++
    Replies:
    3
    Views:
    1,217
    jeffc
    Dec 2, 2003
  3. Guest
    Replies:
    6
    Views:
    2,727
    Siemel Naran
    Jul 15, 2004
  4. derrick
    Replies:
    1
    Views:
    362
    Mike Wahler
    Dec 22, 2004
  5. Lee Xuzhang
    Replies:
    5
    Views:
    340
    Kevin D. Quitt
    Jun 14, 2006
Loading...

Share This Page