CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2002
    Posts
    936

    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

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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,
    Code:
    memmove(y,x,27)
    maybe?

  4. #4
    Join Date
    Jun 2002
    Posts
    936

    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

  5. #5
    Join Date
    Jun 2002
    Posts
    936

    Re: Question about memcpy or memmove

    Quote Originally Posted by treuss
    Uhh,
    Code:
    memmove(y,x,27)
    maybe?

    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.

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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;
    }

  8. #8
    Join Date
    Jun 2002
    Posts
    936

    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.

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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".
    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...

  10. #10
    Join Date
    Jun 2002
    Posts
    936

    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.

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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?"

  12. #12
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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.
    "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
  •  





Click Here to Expand Forum to Full Width

Featured