Thread: memcpy use?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    memcpy use?

    I am using memcpy to copy the contents from buffer1 to buffer2. I use memcpy to copy buffer3 to buffer4.
    My question is this: Is there a way to append buffer4 to buffer2 so that I don't overwrite any of the data in buffer2? or alternatively can I copy buffer2 and buffer4 to another buffer?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by jerrykellyjr
    I am using memcpy to copy the contents from buffer1 to buffer2. I use memcpy to copy buffer3 to buffer4. My question is this: Is there a way to append buffer4 to buffer2 so that I don't overwrite any of the data in buffer2?
    As long as buffer2 has enough room to support its existing contents and buffer4's contents then sure, why not? It is just a matter of knowing how much stuff is already in buffer2 and then copying buffer4's contents to the end of that data in buffer2.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    for instance:
    buffer2 is 1000 bytes in length
    buffer4 is 500 bytes in length

    If I have another buffer called big_buffer which is 2000 bytes in length can I first memcpy buffer2 to big_buffer and then memcpy buffer4 to big_buffer. I actually want buffer4 appended to buffer2 inside of big_buffer.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe something like:

    Code:
    char big_buffer[2000];
    char buffer2[1000];
    char buffer4[500];
    
    ...
    
    memcpy( big_buffer, buffer2, sizeof(buffer2) );
    memcpy( big_buffer+sizeof(buffer2), buffer4, sizeof(buffer4) );
    That should do what you want.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    hk, I see what you mean. Makes sense to me. I'll give it a try...Thanks!

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    Now I get an access violation. Here is what I am trying to do:

    Code:
    struct car_data_struct {
    int blue;
    int green;
    int red;
    float weight;
    float miles_driven;
    }
    
    struct cars_struct {
    int number_of_cars;
    int car_id;
    char car_name[10];
    car_data_struct cardata[50];
    }
    
    struct cars_struct2 {
    int number_of_cars;
    int car_id;
    char car_name[10];
    car_data_struct cardata[50];
    }
    
    struct big_car_struct {
    int number_of_cars;
    int car_id;
    char car_name[10];
    car_data_struct cardata[100];
    }
    
    // in another application I do this:
    car_struct car_buf;
    car_struct2 car_buf2;
    big_car_struct big_car_buf;
    
    memcpy(big_car_buf, car_buf, sizeof(car_struct));
    memcpy(big_car_buf + sizeof(car_struct), car_buf2, sizeof(car_struct2));
    OK I revised my code to show instances.
    What I really want to do in the second memcpy is only copy the cardata from car_buf2 to big_car_buf. Can this be done?
    Last edited by jerrykelleyjr; 02-16-2005 at 03:22 PM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    One big problem, you don't have any instances of the structs to copy to/from. You need to be operating on instances of the struct. Also, since car_struct and car_struct2 are the same, you don't really need two different structs, two instances of a single struct should be fine:

    Code:
    struct color_struct {
    int blue;
    int green;
    int red;
    float weight;
    float miles_driven;
    };
    
    struct cars_struct {
    int number_of_cars;
    int car_id;
    char car_name[10];
    color_struct colors[50];
    };
    
    
    struct big_car_struct {
    int number_of_cars;
    int car_id;
    char car_name[10];
    color_struct colors[100];
    };
    
    car_struct car1, car2;  // Two instances of car_struct
    big_car_struct big_car;  // Single instance of big_car_struct
    You probably don't want to use memcpy in this instance for copying the two car_structs into the one big_car_struct. Think of what you are trying to do by merging them. What is happening when you combine car1 with car2? What is supposed to happen? Do you wish to to add the two number_of_cars variables from car1 and car2 and have the result in the respective variable in big_car? What happens to the car_id field and the car_name value?

    The only somewhat suitable place for you to really use memcpy that I can see is to copy the two 50 element colors array from car1 and car2 into big_car's large 100 element colors array. But many people might just use a loop to do that copying.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    As someone in one of your other threads mentioned (I think it was Prelude?), that sort of thing doesn't work. You'll have to manually copy each variable in each struct somehow (a direct memory copy will NOT work).

    **EDIT**
    Beaten by HK. Listen to him, his explanation is more thorough.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    OK I'm thoroughly confused!
    I want to copy all of car_buf to big_big_car_buf. Then I need to copy only the car_data from car_buf2 to big_car_buf so that it's appended onto the end of the car_buf data. Can this be accomplished? Thanks!

    Here's my revised code:

    Code:
    struct car_data_struct {
    int Car_length;
    int Car_Width;
    int Car_Height;
    float weight;
    float miles_driven;
    }
    
    struct cars_struct {
    int number_of_cars;
    int car_id;
    char car_name[10];
    car_data_struct cardata[50];
    }
    
    struct cars_struct2 {
    int number_of_cars;
    car_data_struct cardata[50];
    }
    
    struct big_car_struct {
    int number_of_cars;
    int car_id;
    char car_name[10];
    car_data_struct cardata[100];
    }
    
    // in another application I do this:
    car_struct car_buf;
    car_struct2 car_buf2;
    big_car_struct big_car_buf;
    
    memcpy(big_car_buf, car_buf, sizeof(car_struct));
    
    memcpy(big_car_buf + sizeof(car_struct), car_buf2, sizeof(car_struct2));
    Last edited by jerrykelleyjr; 02-16-2005 at 04:47 PM.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    big_car_buf.number_of_cars = car_buf.number_of_cars;
    big_car_buf.car_id = car_buf.car_id;
    
    strcpy(big_car_buf.car_name, car_buf.car_name);
    
    int i;  //<-- Edited here
    for(i = 0; i < 50; ++i)
       big_car_buf.car_data_struct[i] = car_buf.car_data_struct[i];
    
    for(; i < 100; ++i)
       big_car_buf.cardata[i] = car_buf2.cardata[i - 50];
    It isn't rocket science, really. memcpy() is useful in many places, and this is not one of them (for the reasons mentioned in the other thread - structs are not compatible, even if you line them up perfectly). Structs may look the same, but internally you usually have little if any idea how they're really put together - so you should generally NEVER attempt a 'copy' in this manner.

    **NOTICE: Edited to correct a small error.
    Last edited by Hunter2; 02-16-2005 at 08:04 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I am using memcpy to copy the contents from buffer1 to buffer2. I use memcpy to copy buffer3 to buffer4.
    I just saw this. You're using the word 'buffer' wrong - what you're dealing with are structures, not buffers. A buffer is usually just an array of bytes, i.e.
    Code:
    char myBuffer[500];  //A buffer of size 500.
    If you're dealing with this sort of thing, then by all means use memcpy - you're just copying an ordered sequence of bytes from one place to another. But with structures, you have no guarantee what size it'll end up with and you have no guarantee of how the data is arranged inside - so unless you're copying from one structure to another structure of exactly the same type, i.e.:
    Code:
    struct A
    {
       int data1;
       int data2;
    };
    
    A struct1;
    A struct2;
    Then you can probably get away with memcpy(), but a simple struct1 = struct2; would suffice anyway so you wouldn't use memcpy() here anyway.

    You will find that memcpy()'s uses are usually limited to more advanced topics.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I have not clearly presented what it is I am trying to do.

    Lets assume that I have two structs that are basically the same struct (ie they both contain the same number of variables of the same types).

    Code:
    struct cars_struct {
    int number_of_cars;
    int car_id;
    char car_name[10];
    car_data_struct cardata[50];
    }
    
    struct cars_struct2 {
    int number_of_cars;
    int car_id;
    char car_name[10];
    car_data_struct cardata[50];
    }
    ,

    The difference is that the second struct has different data values contained in the "cardata" part.

    I was trying to convey that I wanted to copy all of the first struct "car_buf" elements to "big_car_buf".

    Then I wanted to only copy the "cardata" values in "car_buf2" to "big_car_buf" as shown (improperly in the second memcpy I have in my code).

    I need to know first, can this be done using memcpy? Second, can someone please provide an example of how I can do this. Thank you!
    JK

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It can be done using the function memcpy; but it's less efficient, uglier, less safe, and in general completely pointless. I have already posted a fully functional code example of how you can properly do exactly what you have just stated.

    If you want to know how to do it with memcpy:
    Code:
    memcpy(&(big_car_buf.number_of_cars), &(car_buf.number_of_cars), sizeof(int));
    etc. etc. for each member.
    You cannot directly memcpy one struct into another unless they are of the same type, as has been stated about 4 times already. IT DOESN'T MATTER IF THEY HAVE THE SAME VARIABLE TYPES DECLARED IN THE SAME ORDER WITH THE SAME NAMES!!! They are INCOMPATIBLE if they are declared as different structs. If a direct memory copy works at all, it is only by chance and is not by any means guaranteed.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I need to know first, can this be done using memcpy?
    No, not safely. structs of differing types should not be copied over each other.

    >>please provide an example
    hunter2 already has. Copy the individual elements, the use a loop to copy the cardata array elements.

    [beat!]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    In the example hunter gave for the second loop:

    Code:
    for(; i < 100; ++i)
       big_car_buf.cardata[i] = car_buf2.cardata[i - 50];
    will this copy the data to elements 50 through 99 in big_car_buf.cardata[i] ?
    Last edited by jerrykelleyjr; 02-16-2005 at 07:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disagreement about memcpy
    By ch4 in forum C Programming
    Replies: 9
    Last Post: 05-28-2009, 10:12 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM