简体   繁体   English

64 位机器上的 C++ 内存对齐

[英]C++ memory Alignment on a 64 bit machine

Class A
{
  public:
    void fun1();
    void fun2();

  private:
     uint16 temp_var;
};

Is there any reason why I shouldn't just make this variable uint16 to full uint64?有什么理由不应该让这个变量 uint16 变成完整的 uint64? Doing it this way (uin16), do I leave memory "holes" in the object ?, and I'm told the processor is more efficient in dealing with full uint64s.这样做(uin16),我会在对象中留下内存“漏洞”吗?,我被告知处理器在处理完整的 uint64 时效率更高。 for clarification, temp_var is the only member variable.为了澄清起见, temp_var 是唯一的成员变量。 And no where i was using it for size(temp_var) or as a counter to loop back to zero并且没有我将它用于 size(temp_var) 或用作循环回零的计数器

Thank you all for your inputs, appreciate it..谢谢大家的意见,谢谢。。

If the question is, can the compiler do the substitution:如果问题是,编译器是否可以进行替换:

You asked for a uint16 so it gave you a uint16 .你要求一个uint16所以它给了你一个uint16 It would be surprising to get something else.得到其他东西会令人惊讶。

For instance, imagine if a developer was counting on a behavior of integer overflow or underflow.例如,想象一下如果开发人员指望整数上溢或下溢的行为。 In that case, if the compiler substituted a uint64 behind the scenes, then that would be problematically surprising for the developer.在这种情况下,如果编译器在幕后替换了uint64 ,那么对于开发人员来说,这将是一个令人惊讶的问题。

Along the same lines, one would expect sizeof(temp_var) to equal sizeof(uint16) .同样,人们会期望sizeof(temp_var)等于sizeof(uint16)

There are probably further cases in which such a substitution could lead to unexpected behavior that wouldn't be anticipated by the developer.在其他情况下,这种替换可能会导致开发人员无法预料的意外行为。


If the question is, can you the developer pick something else:如果问题是,您是否可以让开发人员选择其他内容:

Sure, you can if you want a variable of that size.当然,如果你想要一个这样大小的变量,你可以。 So then how about some possibilities where you wouldn't...那么,你不会的一些可能性如何......

If you rely on overflow/underflow behavior of a uint16 then of course you would want to stick to that.如果您依赖uint16上溢/下溢行为,那么您当然希望坚持这一点。

Or perhaps this data is going to be passed along to some further location that only supports values in the range of a uint16 , so leaving it that size may make logical sense to try to implicitly document what's valid and/or avoid compiler warnings.或者,也许这些数据将被传递到某个仅支持uint16范围内的值的更远的位置,因此保留该大小可能具有逻辑意义,以尝试隐式记录有效内容和/或避免编译器警告。

Similarly you might want for sizeof(temp_var) to be 2 bytes instead of 8 for some logical reason relevant to other parts of the program.同样,出于与程序其他部分相关的某些逻辑原因,您可能希望sizeof(temp_var)为 2 个字节而不是 8 个字节。

I also expect there are some games one could play with the packing pragma, but I presume that isn't relevant to the intended question.我也希望有一些可以使用包装 pragma 玩的游戏,但我认为这与预期的问题无关。

Depending on the goal of your program, logical consistency or clarity of code may be more important than maximum possible performance (especially at the micro level of being concerned about size/type of a member variable).根据程序的目标,代码的逻辑一致性或清晰度可能比最大可能的性能更重要(尤其是在关注成员变量的大小/类型的微观层面)。 To phrase that another way, uint16 is still fast enough for many many use cases.换句话说, uint16对于许多用例来说仍然足够快。

In some situations though, there won't be any compelling reason to make the decision one way or the other.但是,在某些情况下,不会有任何令人信服的理由以一种或另一种方式做出决定。 At that point, I would go with whatever seems to make the most sense as per personal sensibilities.在这一点上,我会根据个人感受选择任何似乎最有意义的东西。

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

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