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
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
Re: Question about memcpy or memmove
Quote:
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?
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
Re: Question about memcpy or memmove
Quote:
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.
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.
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;
}
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.
Re: Question about memcpy or memmove
Quote:
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?
Quote:
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? ;)
Quote:
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".
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.
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?"
Re: Question about memcpy or memmove
Quote:
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.