Memcpy或struct assignment?

时间:2013-10-07 19:37:53

标签: c struct memcpy memory-alignment

我有以下代码,并且不确定是使用结构对齐还是使用memcpy将struct A复制到自定义“堆栈”char / byte数组。

对于以下两个代码选项或任何完全错误的选项,是否有任何优势/劣势?

必要的结构/功能。

struct B {
    int type;
    struct B *prev;
}

struct A {
    struct B base;
    int n;
    struct B *another;
    char name[1]; /* Struct hack */
};

void align(char **ptr, int n) {
    intptr_t addr = (intptr_t)*ptr;
    if(addr % n != 0) {
        addr += n - addr % n;
        *ptr = (char *)addr;
    }
}

选项1:结构分配

void struct_assignment() {
    char *stack = malloc(400*1000);
    char *top_of_stack = stack + 3149; /* Just an example */

    struct A *var = (struct A *)top_of_stack;
    align((char **)&var, sizeof(struct B)); /* Most restrictive alignment member in struct A */
    var->base.type = 1;
    var->base.prev = NULL;
    var->another = (struct base *)var;
    char *name = "test";
    var->n = strlen(name) + 1;
    strcpy(var->name, name);

    top_of_stack = (char*)var + sizeof(*var)+ (var->n - 1); /* -1 for name[1] */
}

选项2:memcpy

void memcpying() {
    char *stack = malloc(400*1000);
    char *top_of_stack = stack + 3149; /* Just an example */

    struct A var;
    var.base.type = 1;
    var.base.prev = NULL;
    var.another = NULL;
    char *name = "test";
    var.n = strlen(name) + 1;
    strcpy(var.name, name);

    char *aligned_ptr = top_of_stack;
    align(&aligned_ptr, sizeof(struct B)); /* Most restrictive alignment member in struct A */

    memcpy(aligned_ptr, &var, sizeof(var) + (var.n - 1); /* -1 for name[1] */
    struct A *var_ptr = (struct A*)aligned_ptr;
    var_ptr->another = (struct B *)var_ptr;

    top_of_stack = aligned_ptr + sizeof(var)+ (var.n - 1); /* -1 for name[1] */
}

选项1是结构分配吗?

两个选项是否会导致相同的填充和对齐?

目标架构的字节顺序是否会影响选项1?

1 个答案:

答案 0 :(得分:1)

我认为这不能称为struct任务。您正在分配各个字段。

struct在你的情况下你只需要初始化你正在“保留”的堆栈上的对象就可以使用临时代码:

struct base tmp = {
       .type = 1,
       .prev = NULL,
       // whatever other fields you want to initialize
};
var->base = tmp;
使用复合文字

或更加明智:

var->base = (struct base){
       .type = 1,
       .prev = NULL,
       // whatever other fields you want to initialize
};

这两种方法都具有初始化您所有字段的优势 可能忘了0。对于复制操作本身,让编译器选择编译器设计者认为合适的任何东西。除非一些仔细的基准测试告诉你存在真正的问题,否则不要乱搞这些事情。