简体   繁体   English

在已经初始化的结构体结束后如何写?

[英]How to write after the end of the already initialized struct?

I have a struct initialized on a stack, and i want to write data in memory right after the struct and make a pointer inside a struct point to that data.我在堆栈上初始化了一个结构,我想在结构之后立即在 memory 中写入数据,并使结构内的指针指向该数据。 I know it is achievable on the stack/heap with uninitialized structure using malloc(sizeof(struct) + additional size) or alloca().我知道使用 malloc(sizeof(struct) + additional size) 或 alloca() 在未初始化结构的堆栈/堆上是可以实现的。 but can i perform initialization of a data after the struct is already initialized on the stack?但是我可以在结构已经在堆栈上初始化之后执行数据的初始化吗? and can i perform this initialization inside a initializator function?我可以在初始化器 function 中执行此初始化吗?

Simple example:简单的例子:

struct TEST {
    wchar_t* itest;
};

void init_struct(struct TEST* test) {

    // point to the end of the struct
    char* walk_ptr = (char*)test + sizeof(test);
    test->itest = (wchar_t*)walk_ptr;
    
    // initialize data after the struct
    ...
}

int main(void) {
    
    struct TEST test;
    init_struct(&test);

    return 0;
}

You could do this by embedding the structure inside another structure to reserve memory for the extra data:您可以通过将结构嵌入到另一个结构中来为额外数据保留 memory 来做到这一点:

int main(void)
{
    struct { struct TEST test; wchar_t data[NumberOfElements]; } S;
    init_struct(&S.test);
    …
}

However, the code in init_struct adds an incorrect size, sizeof(test) , to the pointer.但是, init_struct中的代码向指针添加了不正确的大小sizeof(test) You likely wanted to add sizeof (struct Test) , or, equivalently, sizeof *test , since you want to get past the struct TEST that test points to, not past a struct TEST * that test is.您可能想添加sizeof (struct Test) ,或者等效地添加sizeof *test ,因为您想通过test指向的struct TEST ,而不是通过test所在的struct TEST *

However, even adding the correct size of the structure would not guarantee strictly conforming C code, since C implementations may insert padding between structure members.但是,即使添加结构的正确大小也不能保证严格符合 C 代码,因为 C 实现可能会在结构成员之间插入填充。 Properly we would add the offset of the data member.适当地,我们将添加data成员的偏移量。 To do that, we nwould eed to give the structure a tag and then either make the structure definition visible to init_struct or pass the offset to init_struct .为此,我们需要给结构一个标签,然后要么使结构定义对init_struct可见,要么将偏移量传递给init_struct However, it is easier just to pass the address of the extra data:但是,只传递额外数据的地址更容易:

void init_struct(struct TEST *test, wchar_t *data)
{
    test->itest = data;
}


int main(void)
{
    struct { struct TEST test; wchar_t data[NumberOfElements]; } S;
    init_struct(&S.test, S.data);
    …
}

Of course, a pointer can point anywhere, and there is no apparent reason the data should be immediate after the structure, so we can disconnect them:当然,指针可以指向任何地方,没有明显的理由数据应该紧跟在结构之后,所以我们可以断开它们:

int main(void)
{
    struct TEST test;
    wchar_t data[NumberOfElements];
    init_struct(&test, data);
    …
}

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

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