浅复制足以用char []结构? [英] Is shallow copy sufficient for structures with char[]?

查看:121
本文介绍了浅复制足以用char []结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字符数组,没有任何其他成员函数的结构。我做的这些结构的两个实例之间分配操作。如果我没有记错的话,也算浅拷贝。是浅拷贝安全在这种情况下?

I have a structure containing character arrays with no any other member functions. I am doing assignment operation between two instances of these structures. If I'm not mistaken, it is doing shallow copy. Is shallow copy safe in this case?

我在C ++中尝试这样做,它的工作,但我只是想确认,如果这种行为是安全的。

I've tried this in C++ and it worked but I would just like to confirm if this behavior is safe.

推荐答案

如果由浅拷贝,你的意思是一个包含数组结构,在转让之后数组将指向原始的结构数据,那么:它​​不能。数组中的每个元素都必须复制到新的结构。 浅拷贝到图片,如果你的结构有指针。如果没有,你的不能的做一个浅拷贝。

If by "shallow copy", you mean that after assignment of a struct containing an array, the array would point to the original struct's data, then: it can't. Each element of the array has to be copied over to the new struct. "Shallow copy" comes into the picture if your struct has pointers. If it doesn't, you can't do a shallow copy.

当您指定一个结构包含数组以一定的价值,它不能做一个浅拷贝,因为这将意味着指派给磁盘阵列,这是违法的。所以,你得到的唯一副本是一个深刻的副本。

When you assign a struct containing an array to some value, it cannot do a shallow copy, since that would mean assigning to an array, which is illegal. So the only copy you get is a deep copy.

考虑:

#include <stdio.h>

struct data {
    char message[6];
};

int main(void)
{
    struct data d1 = { "Hello" };
    struct data d2 = d1; /* struct assignment, (almost) equivalent to
                            memcpy(&d2, &d1, sizeof d2) */

    /* Note that it's illegal to say d2.message = d1.message */

    d2.message[0] = 'h';
    printf("%s\n", d1.message);
    printf("%s\n", d2.message);
    return 0;
}

上面会打印:

Hello
hello

如果,在另一方面,你的结构有一个指针,结构分配只会复制指针,这是浅拷贝

If, on the other hand, your struct had a pointer, struct assignment will only copy pointers, which is "shallow copy":

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct data {
    char *message;
};

int main(void)
{
    struct data d1, d2;
    char *str = malloc(6);
    if (str == NULL) {
        return 1;
    }
    strcpy(str, "Hello");
    d1.message = str;
    d2 = d1;

    d2.message[0] = 'h';
    printf("%s\n", d1.message);
    printf("%s\n", d2.message);
    free(str);
    return 0;
}

上面会打印:

hello
hello

在一般情况下,给结构T D1,D2; D2 = D1; 等同于的memcpy(&放大器; D2,&放大器; D1,sizeof的D2); 的,但如果该结构具有填充,这可能会或可能不会被复制

In general, given struct T d1, d2;, d2 = d1; is equivalent to memcpy(&d2, &d1, sizeof d2);, but if the struct has padding, that may or may not be copied.

修改的:在C中,不能分配到阵列。鉴于:

Edit: In C, you can't assign to arrays. Given:

int data[10] = { 0 };
int data_copy[10];

data_copy = data;

是非法的。所以,正如我上面所说的,如果你有一个数组中结构,分配到结构的必须的复制中的数据元素,明智的数组。你不会在这​​种情况下,应该浅拷贝:它没有任何意义的术语浅拷贝适用于这样的情况下

is illegal. So, as I said above, if you have an array in a struct, assigning to the struct has to copy the data element-wise in the array. You don't get shallow copy in this case: it doesn't make any sense to apply the term "shallow copy" to a case like this.

这篇关于浅复制足以用char []结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆