Obeject

Object

C++ programs create, destroy, refer to, access, and manipulate object.

An object, in C++, is a region of storage that has

  • size(determined with sizeof)
  • alignment requirement(determined with alignof)
  • storage duration(存储时期: automatic, static, dynamic, thread-local)
  • lifetime(bounded by storage duration or temporary)
  • type
  • value(which may be indeterminate, e.g. for default-initialized non-class types)
  • optionally, a name

The following entities are not object: value, reference, function,enumerator, type, class member, bit-fields,template, template specialization, namespace,parameter pack, and this.

Objects are created by definitions, new-expressions, and in several other situations where temporary objects are required: binding a reference to a prvalue, returning a prvalue from a function, a conversion that creates a prvalue, throwing an exception, entering an exception handler, and in some initialization contexts.

Object representation and value representation

For an object of type T, object representation is the sequence (storage duration) of sizeof(T) objects of type unsigned char beginning at the same address as the T object.

sizeof(T)大小的,同一起始地址的unsigned char类型的对象序列作为T对象。

The value representation of an object is the set of bits that hold the value of its type T.

For the objects of type char, signed char, and unsigned char, every bit of the object representation is required to participate in the value representation and each possible bit pattern represents a distinct value (no padding, trap bits, or multiple representations allowed).

Subobjects

An object can contain other objects, which are called subobjects. These include

  • member objects
  • base class subobjects
  • array elements

An object that is not a subobject of another object is called complete object.

Complete objects, member objects, and array elements are also known as most derived objects, to distinguish them from base class subobjects. The size of a most derived object that is not a bit field is required to be non-zero (the size of a base class subobject may be zero: see empty base optimization).

Any two objects (that are not bit fields) are guaranteed to have different addresses unless one of them:

  1. a subobject of another
  2. if they are subobjects of different type within the same complete object-in hierarchy class
  3. one of them is a zero-size base.

Polyomrphic objecets

Objects of class type that declare or inherit at least one virtual function are polymorphic objects.

Within each polymorphic object, the implementation stores additional information (in every existing implementation, it is one pointer unless optimized out), which is used by virtual function calls and by the RTTI features (dynamic_cast and typeid) to determine, at run time, the type with which the object was created, regardless of the expression it is used in.

For non-polymorphic objects, the interpretation of the value is determined from the expression in which the object is used, and is decided at compile time.

#include <iostream>
#include <typeinfo>
struct Base1 {
// polymorphic type: declares a virtual member
virtual ~Base1() {}
};
struct Derived1 : Base1 {
// polymorphic type: inherits a virtual member
}; struct Base2 {
// non-polymorphic type
};
struct Derived2 : Base2 {
// non-polymorphic type
}; int main()
{
Derived1 obj1; // object1 created with type Derived1
Derived2 obj2; // object2 created with type Derived2 Base1& b1 = obj1; // b1 refers to the object obj1
Base2& b2 = obj2; // b2 refers to the object obj2 std::cout << "Expression type of b1: " << typeid(decltype(b1)).name() << ' '
<< "Expression type of b2: " << typeid(decltype(b2)).name() << '\n'
<< "Object type of b1: " << typeid(b1).name() << ' '
<< "Object type of b2: " << typeid(b2).name() << '\n'
<< "size of b1: " << sizeof b1 << ' '
<< "size of b2: " << sizeof b2 << '\n';
}

Output:

Expression type of b1: Base1 Expression type of b2: Base2
Object type of b1: Derived1 Object type of b2: Base2
size of b1: 8 size of b2: 1

###Alignment
Every object type has the property called *alignment requirement*, which is an integer value (of type `std::size_t`, always a power of 2) representing the number of bytes between successive addresses at which objects of this type can be allocated. The *alignment requirement* of a type can be queried with `alignof` or `std::alignment_of`. The pointer alignment function `std::align` can be used to obtain a suitably-aligned pointer within some buffer, and `std::aligned_storage` can be used to obtain suitably-aligned storage.

Each object type imposes its alignment requirement on every object of that type; stricter alignment (with larger alignment requirement) can be requested using alignas.--可以用alignas来强制对齐(通常是为了更大的对齐要求)

In order to satisfy alignment requirements of all non-static members of a class, padding may be inserted after some of its members.

#include <iostream>

// objects of type S can be allocated at any address
// because both S.a and S.b can be allocated at any address
struct S {
char a; // size: 1, alignment: 1
char b; // size: 1, alignment: 1
}; // size: 2, alignment: 1 // objects of type X must be allocated at 4-byte boundaries
// because X.n must be allocated at 4-byte boundaries
// because int's alignment requirement is (usually) 4
struct X {
int n; // size: 4, alignment: 4
char c; // size: 1, alignment: 1
// three bytes padding
}; // size: 8, alignment: 4 int main()
{
std::cout << "sizeof(S) = " << sizeof(S)
<< " alignof(S) = " << alignof(S) << '\n';
std::cout << "sizeof(X) = " << sizeof(X)
<< " alignof(X) = " << alignof(X) << '\n';
}

Output:

sizeof(S) = 2 alignof(S) = 1
sizeof(X) = 8 alignof(X) = 4

The weakest alignment (the smallest alignment requirement) is the alignment of char (typically 1);

the largest fundamental alignment of any type is the alignment of std::max_align_t.

If a type's alignment is made stricter (larger) than std::max_align_t using alignas, it is known as a type with extended alignment requirement.

A type whose alignment is extended or a class type whose non-static data member has extended alignment is an over-aligned type. It is implementation-defined if new-expression, std::allocator::allocate, and std::get_temporary_buffer support over-aligned types.

Allocators instantiated with over-aligned types are allowed to fail to instantiate at compile time, to throw std::bad_alloc at runtime, to silently ignore unsupported alignment requirement, or to handle them correctly.

Objects的更多相关文章

  1. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  2. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  3. [C#] Linq To Objects - 如何操作文件目录

    Linq To Objects - 如何操作文件目录 开篇语: 上次发布的 <LINQ:进阶 - LINQ 标准查询操作概述> 社会反响不错,但自己却始终觉得缺点什么!“纸上得来终觉浅,绝 ...

  4. [C#] Linq To Objects - 如何操作字符串

    Linq To Objects - 如何操作字符串 开篇语: 上次发布的 <LINQ:进阶 - LINQ 标准查询操作概述>(90+赞) 社会反响不错,但自己却始终觉得缺点什么!“纸上得来 ...

  5. 萌新笔记——git的问题(error: object file .git/objects/* is empty...)的解决方案及对git版本库文件的了解

    由于操作不当,导致git版本库出了大问题,如下所示: error: object file .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e ...

  6. JCIP chap3 share objects

    "同步"确保了操作的原子性执行,但它还有其它重要的方面:memory visibility.我们不但要确保当一个线程在使用一个对象的时候,其它线程不能修改这个对象,而且还要保证该线 ...

  7. -[NSNull countByEnumeratingWithState:objects:count:]:

    当数组为空时遍历数组容易出这样的问题, -[NSNull countByEnumeratingWithState:objects:count:]: unrecognized selector sent ...

  8. Create views of OpenCASCADE objects in the Debugger

    Create views of OpenCASCADE objects in the Debugger eryar@163.com Abstract. The Visual Studio Natvis ...

  9. .NET平台开源项目速览(2)Compare .NET Objects对象比较组件

    .NET平台开源项目速览今天介绍一款小巧强大的对象比较组件.可以更详细的获取2个对象的差别,并记录具体差别,比较过程和要求可以灵活配置. .NET开源目录:[目录]本博客其他.NET开源项目文章目录 ...

  10. [c++] Callable Objects

    Five kinds of callable objects: Functions Pointers to functions Objects of a class that overloads () ...

随机推荐

  1. Linux常用的系统监控shell脚本

    http://www.linuxqd.com下面是我常用的几个Linux系统监控的脚本,大家可以根据自己的情况在进行修改,希望能给大家一点帮助.1.查看主机网卡流量 #!/bin/bash #netw ...

  2. poj 2228 Naptime dp

    这个题目的状态还是比较好想的,dp[i][j]表示已经睡了i个时段,最后睡在j时段的最优值,但是需要处理环的情况,我的做法是算两次,第一次不处理环,第二次强制性要求第一个时段需要睡,然后查看dp[m] ...

  3. STL中主要的算法(一)

    一.replace() 替换算法将指定元素值替换为新值,使用原型例如以下,将迭代器[first,last)中值为old_value的元素所有替换为new_value值. 函数原型: template  ...

  4. 启用Spring quartz定时器,导致tomcat服务器自动停止

    在项目中添加了一个定时功能,基于Spring quartz: 设置好执行时间后(如:每天14:00) 当程序执行完后,就会出现以下信息: 2013-7-22 11:36:02 org.apache.c ...

  5. c++ 覆盖、重载、隐藏

    函数重载: 1.相同的范围内(即同一类中) 2.函数名相同: 3.参数不同: 4.virtual关键字可有可无: 函数覆盖:虚函数的功能.动态多态 (父类中必须有virtual)========派生类 ...

  6. Node.js模块os

    OS 操作系统模块 os.hostname() 操作系统的主机名. os.type() 操作系统的名称 os.release() 操作系统的发行版本 os.uptime() 当前系统的时间 以秒为 o ...

  7. js调试若干

    主要是将 chrome调试工具   firebug的控制台对以下都有支持 consoleAPI https://developers.google.com/chrome-developer-tools ...

  8. jQeury学习笔记

    jQuery 语法: 核心语法: $(selector).action() 美元符号定义 jQuery 选择符(selector)"查询"和"查找" HTML ...

  9. hough变换中,直线方程从XY空间转换到参数空间的转换过程

    XY空间直线方程:y=kx+b 参数空间直线方程:xcosθ+ysinθ=ρ 直线方程从XY空间转换到参数空间过程的转换过程: k=tan(π-α)=tan(-α)=-tanα=-cotθ=-cosθ ...

  10. 未能从程序集“System.ServiceModel,xxx”中加载类型“System.ServiceModel.Activation.HttpModule”。

    一.平台环境 二.问题描述 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561 ...