gpt4 book ai didi

c++ - 是否保证 sizeof(T[N]) == N * sizeof(T)?

转载 作者:可可西里 更新时间:2023-11-01 17:34:31 24 4
gpt4 key购买 nike

我一直假设 T 类型的 N 个元素的数组的大小,如 sizeof 返回的那样,保证正好是 sizeof( T)

comments on this question虽然让我怀疑。有信誉的用户声称数组可能包含填充,这会破坏相等性。当然,这样的平台可能不存在,但它们被允许吗?

如果允许,这将打破许多常见的习语,例如使用 N * sizeof(T) 计算数组所需的存储空间,或使用 计算数组中的元素数量sizeof(a)/sizeof(a[0])

最佳答案

是的。 [expr.sizeof] 包含关于 sizeof 的这一点:

When applied to an array, the result is the total number of bytes in the array. This implies that the size of an array of n elements is n times the size of an element.

关于c++ - 是否保证 sizeof(T[N]) == N * sizeof(T)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com