- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面给出对象表示的定义(3.9/4):
The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T).
但是 3.9.1/1 说:
A
char
, asigned char
, and anunsigned char
occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation.
看起来对象表示取决于对齐要求。但是我引用的定义中没有提到它。那是两个相同大小的对象可能具有不同的对象表示,是吗?
基本上,我问的是以下内容:假设我们有两个大小相同的对象,其中一个对象的对齐方式与另一个对象不同。例如:
struct A
{
char a;
char b;
char c;
char d;
};
A a; //Object 1. alignof(a) = 1
int b; //Object 2. alignof(b) = 4
这些对象是否具有相同的对象表示?
最佳答案
关于对齐的工作原理存在一些混淆。
你是对的,对象表示取决于对齐要求:
object representation is the sequence of sizeof(T) objects of type unsigned char
同时
The value representation of an object is the set of bits that hold the value of its type T
如果您采用以下类型:
struct S {
char c; // 1 byte value
// 3 bytes padding
float f; // 4 bytes value
bool operator==(const S& arg) const { // value-based equality
return c == arg.c && f == arg.f;
}
};
assert(sizeof(S) == 8); // object representation
对象表示大小占8个字节,而值表示大小只占5个字节,它决定了对象相对于另一个对象的值。存在导致差异的对齐要求,并且它们引入了一些填充。
在您的示例中,对象表示和值表示的类型具有相同的大小,加上对象表示大小等于值表示大小。
struct A
{
char a;
char b;
char c;
char d;
};
A a; //Object 1. Object representation size = 4, value representation size = 4, alignof(a) = 1
int b; //Object 2. Object representation size = 4, value representation size = 4, alignof(b) = 4
这里的不同之处在于存储和访问对象所需的对齐方式。
在某些处理器上访问非 4 字节对齐地址上的 4 字节整数会产生 fatal error 。您的代码段中使用的 alignof
关键字正是这样说的:您需要在 1 字节对齐的地址(即任何地方)上分配 A 类型的对象,因为您要访问子对象单个字节作为最大单次读取,它们可以安全地在任何地方访问,无论如何你需要在 4 字节对齐的地址上分配一个整数,以便我安全地访问它。
§3.11/p1
Object types have alignment requirements (3.9.1, 3.9.2) which place restrictions on the addresses at which an object of that type may be allocated. An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated
这意味着:您应该在 4 字节对齐的地址上分配一个整数,但是从缓冲区开始到对象开始所需的字节不会成为对象的一部分:
|0x07|0x08|0x09|0x0A|0x0B|..
> I can allocate an integer here, on a 4-bytes-aligned address
> Here the buffer starts
|------------------|
object rep size == value rep size
请注意,如果不满足对齐要求会发生什么取决于系统,即在 x86 上你会遇到性能下降,使用 SSE 你可能会崩溃,在 GPU 内存空间上你将无法恢复你的程序。该标准仅规定了关于对齐分配请求应该发生的事情:
If a request for a specific extended alignment in a specific context is not supported by an implementation, the program is ill-formed. Additionally, a request for runtime allocation of dynamic storage for which the requested alignment cannot be honored shall be treated as an allocation failure.
关于c++ - 具有不同对齐要求的不同类型的两个对象可以具有相同的对象表示吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26200639/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!