|
-
June 26th, 2007, 03:04 PM
#1
Question about memcpy or memmove
It seems like memcpy and memmove can't copy source string to destination string if the sizes are not the same. I have the following sgring
source x[27] and destination y[256]
I want to copy or move the contain of x to y, but keep getting error source is too small
memmove(y, x, 256);//keep getting error, x is 27 bytes too small
so how can I move the data
-
June 26th, 2007, 03:34 PM
#2
Re: Question about memcpy or memmove
Well, the command:
Code:
memmove (y, x, 256);
says, "move 256 bytes from x into y." Is x 256 bytes in length?
Viggy
-
June 26th, 2007, 04:06 PM
#3
Re: Question about memcpy or memmove
 Originally Posted by vcstarter
memmove(y, x, 256);//keep getting error, x is 27 bytes too small
so how can I move the data
Uhh,maybe?
-
June 26th, 2007, 04:06 PM
#4
Re: Question about memcpy or memmove
No, the length of the source is 27, however I want to duplicate the length until it reach 256 which is the length of the destination.
for instance assume that source is [1 2 3]; now I want my destination to be something like that destination[1 2 3;1 2 3;1 2 3 and soth forth; basically, I want to duplicate the destionation from the source several times
-
June 26th, 2007, 04:08 PM
#5
Re: Question about memcpy or memmove
 Originally Posted by treuss
I don't think that will duplicate the destination. I think that will simply create a copy of the length 27. Although I haven't tried that yet, but I don't think it will do it.
-
June 26th, 2007, 04:16 PM
#6
Re: Question about memcpy or memmove
If you want source to be duplicated enough times to fill destination you have to write that code yourself using repeated calls to memmove (or memcpy). Be careful with the last call though, it will overflow destination if number of bytes is not reduced from 27.
-
June 26th, 2007, 06:17 PM
#7
Re: Question about memcpy or memmove
Maybe write an "<algorithm> - type" function:
Code:
#include <string>
#include <iostream>
template <typename Source , typename Dest>
void RepeatCopy(Source sit , Source sit_e , Dest dit , Dest dit_e)
{
Source start = sit;
while (dit != dit_e)
{
*dit = *sit;
++sit;
++dit;
if (sit == sit_e) sit = start;
}
}
int main()
{
using namespace std;
string s = "abc";
string d = "1234567890123456789";
RepeatCopy(s.begin(),s.end(),d.begin(),d.end());
cout << d << "\n";
int xs [] = {1,2,3,4};
int xd [] = {9,9,9,9,9,9,9,9,9,9,9,9,9,9};
RepeatCopy(xs,xs+4,xd,xd+14);
for (int i=0; i<14; ++i)
cout << xd[i];
cout << "\n";
return 0;
}
-
June 27th, 2007, 08:51 AM
#8
Re: Question about memcpy or memmove
I am using a c compiler that cannot compile c++. Anyway, I found another way of doing something different. I am reading and writing data to a serial port. memcpy and memmove did not work. I tried to use them to duplicate the data several time, however when I look my buffer before sending the data out, it looks good. However it send out jagg's. So, I use something like memset. I don't know why memset works better than memmove and memcpy. I think they should be the same.
-
June 27th, 2007, 11:26 AM
#9
Re: Question about memcpy or memmove
 Originally Posted by vcstarter
...I am reading and writing data to a serial port. memcpy and memmove did not work...
What do you mean - "didn't work"? Can you show the code that didn't work?
 Originally Posted by vcstarter
...however when I look my buffer before sending the data out, it looks good. However it send out jagg's...
If the buffer looks good, may be you are sending it wrong? 
 Originally Posted by vcstarter
...So, I use something like memset. I don't know why memset works better than memmove and memcpy. I think they should be the same.
memset doesn't work "better" than memcpy or memmove, it just does different thing. And NO, these functions should NOT be "the same".
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
June 27th, 2007, 02:18 PM
#10
Re: Question about memcpy or memmove
Basically what I mean, I use memcpy and memmove to duplicate the data in the form of for instance, if I have x[1 2 3]; now after after copy x to y multiple time, y looks like [1 2 3 \0 1 2 3 \0 etc.], now when I send y to the serial port, I get jagg's. But when I use memset to set the buffer to one character and sent it, there is no problem. This is the same problem for instance, if I initialize my data like y="a b c" and send it, no problem, but if I use a for loop to initialize the data and sent it, I receive control characters instead of the real data.
-
June 27th, 2007, 02:28 PM
#11
Re: Question about memcpy or memmove
You can't blame memcpy/memmove for not knowing what you would like them to do. 
If you receive control characters you have sent control characters so I join in by posting the soon to be most common phrase at CG: "Can you show the code that do wrong?"
-
June 27th, 2007, 03:40 PM
#12
Re: Question about memcpy or memmove
 Originally Posted by vcstarter
But when I use memset to set the buffer to one character and sent it, there is no problem.
Sending data that contains a replicated single character is not the same as sending data that contains many different characters.
For example, the character '\0' is likely to do harm, when send to character devices.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|