DEVELOP > C > RE: Need help with memcpy_rom funtion
| Topic: |
DEVELOP > C |
| User: |
"" |
| Date: |
05 Feb 2008 03:07:57 AM |
| Object: |
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?
.
|
|
| User: "CBFalconer" |
|
| Title: Re: Need help with memcpy_rom funtion |
05 Feb 2008 04:14:41 PM |
|
|
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
.
|
|
|
|
| User: "=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=" |
|
| Title: RE: Need help with memcpy_rom funtion |
05 Feb 2008 03:14:45 AM |
|
|
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
.
|
|
|
| User: "" |
|
| Title: Re: Need help with memcpy_rom funtion |
05 Feb 2008 04:10:39 AM |
|
|
On Feb 5, 11:14=A0am, "Tom=E1s =D3 h=C9ilidhe" <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)
/*
=A0 Description
=A0 =A0 Copy n bytes of memory from s2 to s1.
=A0 =A0 =A0 Destination: =A0 s1
=A0 =A0 =A0 Source: =A0 =A0 =A0 =A0 s2
=A0 =A0 =A0 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> =A0 =A0/* size_t */
void *memcpy(void *const dest,void const *const src,size_t len)
{
=A0 =A0 char unsigned *pdest =3D dest, *psrc =3D src;
=A0 =A0 while (len--) *pdest++ =3D *psrc++;
=A0 =A0 return dest;
}
char const hello[] =3D "Hello";
int main(void)
{
=A0 =A0 char buf[12];
=A0 =A0 memcpy(buf,hello,sizeof hello);
=A0 =A0 return 0;
}
--
Tom=E1s =D3 h=C9ilidhe
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.
.
|
|
|
|
| User: "pete" |
|
| Title: Re: Need help with memcpy_rom funtion |
05 Feb 2008 05:20:08 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.
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
.
|
|
|
|
|

|
Related Articles |
|
|