Is it safe to use memcpy on struct with const members?

Viewed 99

Assume this struct:

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

Is this code safe?

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

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

I printed the content of the fields, and they have the desired values. And I compiled with -Wall -Wextra -pedantic and I did not get any warnings. But I'm worried this might be undefined behavior or something. Is this safe to do?

0 Answers
Related