简体   繁体   English

Memcpy或结构分配?

[英]Memcpy or struct assignment?

I have the following code and am unsure of whether to use structure alignment or memcpy to copy struct A onto the custom "stack" char/byte array. 我有以下代码,不确定是否要使用结构对齐或memcpy将结构A复制到自定义“堆栈”字符/字节数组上。

Is there anything advantageous/disadvantageous about the following two options of code or anything that is just flat out wrong? 以下两种代码选择是否有任何优缺点?

The necessary struct/functions. 必要的结构/功能。

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;
    }
}

Option 1: Struct Assignment 选项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] */
}

Option 2: memcpy 选项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] */
}

Is option 1 even struct assignment? 选项1甚至是结构分配吗?

Will both options result in the same padding and alignment? 两种选择都会导致相同的填充和对齐方式吗?

Will the endianness of the target architecture affect option 1? 目标体系结构的字节序是否会影响选项1?

I don't think that this can be called struct assignment. 我不认为这可以称为struct分配。 You are assigning to the individual fields. 您正在分配给各个字段。

struct assingment in your case where you are merely interested in initialization of the object on the stack that you are "reserving" could use a temporary: 在您仅对“保留”堆栈上的对象初始化感兴趣的情况下,进行struct设置可以使用临时方法:

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

or even more consise by using a compound literal: 甚至可以通过使用复合文字来简化:

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

Both methods have the advantage of initializing all fields that you might have forgotten to 0 . 两种方法都具有初始化您可能忘记为0所有字段的优点。 The for the copy operation itself, let the compiler chose whatever the compiler designer saw fit. 对于复制操作本身,让编译器选择编译器设计者认为合适的任何东西。 Don't mess around with such things unless some careful benchmarking tells you that there is a real problem. 除非有些仔细的基准测试告诉您这是一个真正的问题,否则请不要弄乱这些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM