简体   繁体   English

结构或指针的结构?

[英]Struct of struct or pointer?

What is the best/fastest way ? 最好/最快的方法是什么?

Struct of struct: 结构的结构:

struct Sheet{
    u8 status; // to check if empty ?
    u8  reserve0;
    u8  reserve1;
    u8  reserve2;
    struct words words[1024];
};

struct Book {
    int id;
    struct Sheet sheet[64];
};

In this case, how to check if the sheet table is empty or not ? 在这种情况下,如何检查sheet的表是空的或不是? I need to add a status for each sheet ? 我需要为每张纸添加status吗?

or 要么

Pointer table of struct 指针结构表

struct Sheet{
    u8  reserve0;
    u8  reserve1;
    u8  reserve2;
    struct words words[1024];
};

struct Book {
    int id;
    struct Sheet* sheet[64];
};

I don't need to use malloc since they are fixed table. 我不需要使用malloc因为它们是固定表。

In the struct of struct , I can initialize by setting status but for pointer to struct , I can initialize with bookinstance.sheet[] = NULL or something like this. struct的struct中 ,我可以通过设置status进行初始化,但是对于struct的指针 ,我可以使用bookinstance.sheet[] = NULL或类似的东西进行初始化。

I am pretty lost with pointers , struct and malloc . 我对pointersstructmalloc感到很malloc I come from Ruby ... 我来自Ruby ...

To be clear: 要清楚:

I want not more than 64 sheet in my book instance. 我的book实例不超过64 sheet Maybe only 3 will be used or 64... But I want to be able to add them and check them with a number from 0 to 63. 也许只使用3个或使用64个...但是我希望能够添加它们并使用0到63之间的数字进行检查。

To a certain extent it depend on what you mean by "faster". 在某种程度上,它取决于您所说的“更快”。 How your code will interact with these elements will change that. 您的代码如何与这些元素交互将改变这种情况。 In general; 一般来说; however, there are very few times you would likely see any significant effect of CPU time. 但是,几乎没有机会看到CPU时间的任何重大影响。

Note that in option 2 Book only allocates an array of pointers so you will have to add code (and overhead) to allocate Sheets for the pointers to point to. 请注意,在选项2中,Book仅分配了一个指针数组,因此您将不得不添加代码(和开销)来分配Sheets以便将指针指向。

The first option would seem cleaner as long as you are not intending on some Sheets to be shared among books. 只要您不打算在某些图纸之间共享书本,第一种选择似乎比较干净。

Also, if you are copying books, option 2 would be faster since you would only be copying pointer rather then the entire Sheet struct. 同样,如果您要复制书籍,则选项2会更快,因为您将只复制指针而不是整个Sheet结构。

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

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