简体   繁体   English

更好地传递struct或指向struct的指针?

[英]Better to pass struct, or pointer to struct?

I have a data struct, that will be read in a function. 我有一个数据结构,将在函数中读取。

I want the smallest memory, code size, and speed footprint possible. 我想要最小的内存,代码大小和速度占用空间。 I'm working on AVR. 我正在制作AVR。

typedef struct {
    uint16_t clu;
    uint16_t num;
    uint32_t cur_rel;
} FSAVEPOS;

Now, a function that stores file position into the struct: 现在,一个将文件位置存储到结构中的函数:

// Approach 1
FSAVEPOS save_pos(const FFILE* file); // return value

// Approach 2
void save_pos(const FFILE* file, FSAVEPOS* pos); // modify reference

And a function that reverses this (FFILE object is modified): 和一个与此相反的函数(修改了FFILE对象):

// Approach 1
void restore_pos(FFILE* file, const FSAVEPOS pos); // pass by value

// Approach 2
void restore_pos(FFILE* file, const FSAVEPOS* pos); // pass by reference

What would you advise as the best idea? 您会建议什么是最好的主意?

If you are trying to minimise memory footprint, then a struct type that is significantly larger than a pointer is better passed using a pointer. 如果要尽量减少内存占用,则最好使用指针传递比指针大得多的struct类型。 Data that is significantly smaller is better passed by value. 较小的数据最好按值传递。 If a pointer and the data are about the same size, it doesn't matter much. 如果指针和数据的大小大约相同,则没关系。

Assuming you are on AVR32, your pointer will be 32-bit and the struct is 64-bit (plus any padding). 假设您使用的是AVR32,则指针将为32位,结构为64位(加上任何填充)。

That would suggest you are better passing by pointer/reference. 那将建议您最好通过指针/引用传递。 However, the struct is not particularly large, other considerations may dominate - there is not really a lot in it for your struct type, as it is not particularly large. 但是,该结构不是特别大,其他因素可能会占主导地位-您的struct类型实际上并不多,因为它不是特别大。 You would need to measure relevant quantities (memory usage, code size, speed, etc) to be sure. 您需要测量相关数量(内存使用量,代码大小,速度等)以确保确定。

So the best idea, I suggest, is to measure - these things are affected by host architecture, compiler settings, quality of implementation of the compiler, etc. 因此,我建议最好的方法是进行测量-这些因素受主机体系结构,编译器设置,编译器的实现质量等影响。

Although you haven't asked, larger types (like uint32_t ) tend to have larger alignment requirements than smaller types (like uint16_t ). 尽管您没有要求,但较大的类型(如uint32_t )比较小的类型(如uint16_t )具有更大的对齐要求。 A consequence of this is a fairly common guideline of ordering members of your struct so larger types appear first in memory. 结果是相当普遍的结构成员排序准则,因此较大的类型首先出现在内存中。 This is probably not significant in your case either (it is a fair bet that a 16 bit type will be aligned to 16 bit boundaries, and 32-bit types to 32-bit boundaries, so it is likely there will be no padding in your case). 这在您的情况下也可能并不重要(可以肯定的是,16位类型将与16位边界对齐,而32位类型将与32位边界对齐,因此很可能在您的表中没有填充案件)。 However, if you had three uint16_t members rather than two, you would be better off ordering things so the uint32_t members are first. 但是,如果您有3个uint16_t成员而不是2个,则最好订购东西,因此uint32_t成员优先。 In other words, instead of 换句话说,代替

typedef struct
{
    uint16_t a,b,c;
    uint32_t d;
} SOME_TYPE;

you would be better off using a different order. 您最好使用其他顺序。

typedef struct
{
    uint32_t d;
    uint16_t a,b,c;
} SOME_TYPE;

In the first case, when you return a struct in C, it's usually returned in a register if it fits or is implicitly passed by reference and modified AFAIK. 在第一种情况下,当您使用C返回结构时,如果它适合或被引用和修改后的AFAIK隐式传递,则通常会在寄存器中返回它。 So, in the best case it's better and in the worst case is equal than the second approach. 因此,在最好的情况下,它比第二种方法更好,而在最坏的情况下,它等于第二种方法。

In the second case it depends on the size of a pointer and the size of the struct among other things. 在第二种情况下,它取决于指针的大小和结构的大小。 In this case is very difficult to say anything without measurement. 在这种情况下,如果不测量就很难说什么。 It probably won't be much different with a struct of that size, though. 但是,使用这种大小的结构可能不会有太大不同。

However you should consider also having consistency with the rest of your API, and having a system to notify errors if you can't do the operation (if the operation can fail). 但是,您还应该考虑与API的其余部分保持一致,并且如果无法执行操作(如果操作可能失败),则需要有一个系统来通知错误。

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

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