小编A L*_*Lan的帖子

复制简单结构时memcpy和'='之间的区别

考虑复制一个不需要特殊复制语义的简单结构.

struct A
{
    char i
    int i;
    long l;
    double b;
    //...maybe more member
}
struct A a;
a.c = 'a'; //skip other member just for illustrate
struct A b;
memset(&a, 0, sizeof(a));
b.c = a.c;
//...for other members, the first way to assign
memcpy(&b, &a, sizeof(b)); //the second way
b = a;    //the third way
Run Code Online (Sandbox Code Playgroud)

3种方法做同样的事情,似乎所有这些方法都是正确的.我曾经使用'memcpy'来复制简单的结构,但现在似乎'='可以做同样的事情.那么使用memcpy和'=' 之间有什么区别吗?

c c++ struct memcpy

2
推荐指数
1
解决办法
759
查看次数

标签 统计

c ×1

c++ ×1

memcpy ×1

struct ×1