Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   RE: Need help with memcpy_rom funtion (http://www.velocityreviews.com/forums/t589598-re-need-help-with-memcpy_rom-funtion.html)

Rudolf.Erasmus@za.saabgroup.com 02-05-2008 09:07 AM

RE: Need help with memcpy_rom funtion
 
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?

Tomás Ó hÉilidhe 02-05-2008 09:14 AM

RE: Need help with memcpy_rom funtion
 
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

Rudolf.Erasmus@za.saabgroup.com 02-05-2008 10:10 AM

Re: Need help with memcpy_rom funtion
 
On Feb 5, 11:14*am, "Tomás Ó hÉilidhe" <t...@lavabit.com> 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.

pete 02-05-2008 11:20 AM

Re: Need help with memcpy_rom funtion
 
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

CBFalconer 02-05-2008 10:14 PM

Re: Need help with memcpy_rom funtion
 
Rudolf.Erasmus@za.saabgroup.com 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



All times are GMT. The time now is 10:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.