1

If I have a structure defined like:

struct image{
unsigned int width, height;
unsigned char *data;
};

And 2 variables of this type:

struct image image1;
struct image image2;

I want to transfer the data from image1 to the data of image2(presuming image1 has some data written, and image2 has data allocated with malloc or calloc). How can it be done? Thanks a lot.

5 Answers 5

5

Assuming it is undesirable that two instances of struct image are pointing to the same data then memcpy() cannot be used to copy the structs. To copy:

  • allocate memory for destination struct
  • allocate memory for destination data buffer based on source data
  • assign width members
  • memcpy() data members.

For example:

struct image* deep_copy_image(const struct image* img)
{
    struct image* result = malloc(sizeof(*result));
    if (result)
    {
        /* Assuming 'width' means "number of elements" in 'data'. */
        result->width = img->width;
        result->data = malloc(img->width);
        if (result->data)
        {
            memcpy(result->data, img->data, result->width);
        }
        else
        {
            free(result);
            result = NULL;
        }
    }
    return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

assign height member. result->height, typo?
2

did you try memcpy(&image1,&image2,sizeof(image));

edit: Alocate data for image2.data after that you have to strcpy(image2.data,image1.data) if data is null terminated, but if its not, then use memcpy with the size of data.

Regards, Luka

1 Comment

There's no typedef of struct image so that sizeof will throw an error.
2
struct image image1;
struct image image2;

...

image2.width = image1.width;
image2.height = image1.height;

/* assuming data size is width*height bytes, and image2.data has enough space allocated: */

memcpy(image2.data, image1.data, width*height);

Comments

1

If all you want is duplicating the struct (i. e. creating a "shallow" copy):

image2 = image1;

if you also want to copy the data pointed to by image1.data ("deep copy"), then you need to do that manually:

memcpy(image2.data, image1.data, image1.width * image1.height);

(assuming there are image1.width * image1.height bytes in the data, and there's enough space malloc()ated in image2.data for storing that.)

3 Comments

With this image2.data will point to image1.data which is not correct. Each member should be copied separately.
@H2CO3 The shallow copy section might benefit from a mention that any allocated data member of image2 would be leaked.
@Rohan Huh what? "With this" - with what?
1

It's simple in C. Simply do the following (assuming image2 is uninitialized):

image2 = image1;  //there's no deep copy, pointed to memory isn't copied

You can assign one structure variable to other given they are of the same type. No need to copy piece-meal. This is a useful feature of C.

This has been discussed before:

Assign one struct to another in C

9 Comments

-1. Sorry, but this leaks any existing data member of image2. If you assume that image2 is effectively uninitialised before this assignment, it'll be important to say this explicitly.
@simonc You contradict the link above, and the answer with 26 upvotes.
I don't think I contradict that question - this is just a slightly more complex situation. The most popular answer to that question hints at the problems you'll run into when your structs own dynamically allocated memory.
@simonc Before we wrap up,please give me a short but rigorous difference between shallow copy and deep copy.I think I am fuzzy about that.
Shallow copy - copy struct members, including addresses of pointer members. Deep copy - copy by-value members, allocate copies of pointer members that own memory, copy addresses of pointer members that don't own memory. Does that make sense?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.