在 C 中的特定地址创建结构的正确方法是什么?

What is the correct way to create a struct at a specific address in C?

C++ 有一个巧妙的功能,您可以在特定内存地址创建一个对象,如 shown here

这对 mmap 特别有用,如下所示:

void *ptr = mmap(0, length, prot, flags, fd, offset);

A *ptrA = new (ptr) A;

仅使用 C 风格结构复制它的正确方法是什么?会不会只是这个?

void *ptr = mmap(0, sizeof(A), prot, flags, fd, offset);

A *ptrA = (A*) ptr;

这是有效的,因为在 C 中没有强制执行内存使用限制。

void *ptr = mmap(0, sizeof(A), prot, flags, fd, offset);

A *ptrA = (A*) ptr;