在具有const成员的struct上使用memcpy是否安全?

问题描述

假设此结构:

struct my_struct {
    const int x;
    const int y;
};

此代码安全吗?

struct my_struct *s = malloc(sizeof *s); // Assuming it succeeds

memcpy(s,&(struct my_struct) {.x = 4,.y=6},sizeof *s);

我打印了字段的内容,它们具有所需的值。而且我使用-Wall -Wextra -pedantic进行了编译,但没有收到任何警告。但是我担心这可能是未定义的行为。这样安全吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)