技术上对象可以占用非连续的存储字节吗? [英] Can technically objects occupy non-contiguous bytes of storage?

查看:125
本文介绍了技术上对象可以占用非连续的存储字节吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答此问题时,我被要求提供标准报价。我惊讶地发现在C ++ 14草案:


§3.9类型[basic.types]

$ b $类型T的对象的对象表示是由类型T的对象占用的N个unsigned char对象的序列,其中N
equals sizeof(T)


char对象必须在内存中连续。也许它是由序列暗示。然后我发现了一个具体的连续字节存储,但...


§1.8 C ++对象模型[intro.object ]


  1. [...]普通可复制或标准布局类型(3.9)的对象应占用连续字节

什么?只有普通的可复制和标准布局类型才需要占用连续的存储字节?其他类型可以在他们占用的存储空间中有漏洞?我搜索其余的标准,但没有找到任何其他相关性连续存储。



如果这是真的(对我来说,这将是对标准最大的冲击)如何和 sizeof 和指针算术吗?是否真的有任何架构/编译器使用(使用)不连续的存储字节类型?



我真的希望我误解和/ 。






编辑:我认为也许它与填充有关,但是这种解释只有在简单可复制标准布局类型不能有填充。然后你说这些类型占据连续字节存储,对于可以有padding的其他类型,你不说。但是,显然不是这样,因为任何结构类型都可以有填充。

解决方案

在内存的连续字节中(因为虚拟基础和类的其余部分之间的一些字节可以被从虚拟基础派生的另一个类占据。

  class A {
int a;
};

B类:virtual A {
int b;
};

class C:virtual A {
int c;
};

class D:public B,C {
int D;
}

类D的对象的内存可以组织成这样:

  ------- 
| a |
-------
| b |
-------
| c |
-------
| d |
对象C,其由整数a和a组成,其中c和c,不占用连续的内存位置。


While answering this question I was asked to provide standard quotes. I was shocked to find in the C++14 draft:

§ 3.9 Types [basic.types]

  1. 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)

Hmm.. it doesn't say that the "unsigned char objects" must be contiguous in memory. Maybe it is implied by "sequence". Then I found a specific mention of "contiguous bytes of storage", but...

§ 1.8 The C++ object model [intro.object]

  1. [...] An object of trivially copyable or standard-layout type (3.9) shall occupy contiguous bytes of storage.

What? Only trivially copyable and standard-layout types are required to occupy contiguous bytes of storage? The rest of the types can have "holes" in the storage they occupy? I searched the rest of the standard but could not find any other relevance to "contiguous storage". Granted I am not that familiar with the standard.

If that is true (for me it would be the greatest shock about the standard) how does that go with sizeof and pointer arithmetics? Are (were) there really any architectures/compilers that use (used) non-contiguous bytes of storage for types?

I really hope I am misinterpreting and/or missing something.


edit: I thought that maybe it is related to padding, but this interpretation would make sense only if trivially copyable or standard-layout types could not have padding. Then you say these types occupy contiguous bytes storage, and for the rest of types who can have padding you don't say that. But that is clearly not the case as any struct type can have padding.

解决方案

A class with a virtual base class may not be in contiguous bytes of memory (as some of the bytes between the virtual base and the rest of the class can be occupied by another class that also derives from the virtual base.

class A {
    int a;
};

class B: virtual A {
    int b;
};

class C: virtual A {
    int c;
};

class D: public B, C {
    int D;
}

The memory for an object of class D can be organized something like this:

-------
|  a  |
-------
|  b  |
-------
|  c  |
-------
|  d  |
-------

in increasing memory order. Object "C", which consists of ints "a" and "c", does not occupy consecutive memory locations.

这篇关于技术上对象可以占用非连续的存储字节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆