对于用户没有定义dctor(包括其所有成员)的类来说, new CLASS[n] 可能会直接请求sizeof(CLASS)*n的空间.

而带有dctor的 类, 因为delete[]的时候要逐个调用析构函数, 要保证调用n次析构.

C++标准没有指定如何具体处理这种情况. 而对于很多数编译器来说, 是在请求的内存前面, 保存对象的个数n(放在其头部).

sizeof(int)  sizeof(CLASS)  ...      
n Object[0] Object[1] Object[2] ... Object[n-1]

on new[]:

1.call CLASS::operator new[], or ::operator new[] ( n*sizeof(CLASS) + sizeof(int) ) to allocate memory

2.write number n to first memory address

3.offset base in sizeof(int), to locate array base(beginning)

4.call ctor for all objects

on delete[] (ptr)

1.real base: offset ptr in -sizeof(int)

2.read the array element count 'n' in real memory base

3.for each object in array ptr, call dctor for 'n' times

4.call CLASS::operator delete[] , or ::operator delete[] (real base) to free memory

基本原理就是上面这样. 对于处理algined的数据, 编译器需要做额外处理:

1. 要求CLASS::operator new[] 分配出的内存已经是对齐的, 编译器假定它分配的是对齐的内存, 也就是说, 用户实现的CLASS::operator new[] , 需要使用对齐分配.

 struct __declspec(align()) Test
{
...
void* operator new[](size_t bytes)
{
return _aligned_malloc(bytes, 16);
}
};

2.sizeof(CLASS) 是对齐后的值, 否则无法保证array中后续所有元素都是对齐的.

3.在上面的基础上, 编译器请求的内存大小有轻微区别:

请求的内存数量稍微偏多:n*sizeof(CLASS) + sizeof(int) =>  n* sizeof(CLASS) + aligned( sizeof(int) )

注意上面, 前后的 sizeof(CLASS) 的值是不一样的, 前者没有对齐, 后者是对齐后的, 也就是说加了alignment以后, sizeof()的值不一样.

4.偏移量也需要对齐, 从而保证偏移以后的base address是对齐的: 即offset不再是sizeof(int), 而是aligned(sizeof(int))

这样可以保证最终使用的数组是对齐的.

aligned( sizeof(int) )  sizeof(CLASS) : aligned  ...      
n Object[0] Object[1] Object[2] ... Object[n-1]

 

上面的除了operator new[] 需要用户实现以外, 编译器都会把剩余的工作做完.

经过测试MSVC11和MinGW GCC 4.8 都是没有问题的.

但是在android的GCC4.6上是有bug的, offset忽略了对齐的问题, 使用了固定偏移量8. 解决方y法是hack, 在operator new[]/delete[]里面手动处理offest.

最新文章

  1. Ubuntun CentOS的ISO官方MD5在哪里查看(安装虚拟电脑时出现严重错误的解决方法)
  2. 《java编程思想》读书笔记 暂停一段时间,改为上面的练习题
  3. http://www.miniui.com/demo/#src=datagrid/celledit.html
  4. C# Winform程序获取外网IP地址
  5. 实验室中搭建Spark集群和PyCUDA开发环境
  6. iOS开发——锁屏监听
  7. [BEC][hujiang] Lesson04 Unit1:Working life ---Reading + Listening &Grammar & Speaking
  8. Bluetooth LE(低功耗蓝牙) - 第二部分
  9. juce Justification 分析
  10. HDU4707:Pet(DFS)
  11. centOS静态ip设置
  12. WPF控件 在XP下获得焦点有虚线框
  13. Dynamics CRM 本地插件注册器连CRMAn unsecured or incorrectly secured fault was received from the other party
  14. Vue的安装及使用快速入门
  15. 环境搭建 - Maven(Windows)
  16. checkbox,三种状态设置
  17. E. Binary Numbers AND Sum
  18. docker在centos7系统镜像下遇到的一些问题
  19. [Algorithm] Maximum Flow
  20. TF-IDF模型的概率解释

热门文章

  1. 生成不重复随机数,int转 TCHAR 打印输出
  2. Android Material Design:基于CoordinatorLayout实现向上滚动导航条ToolBar滚出、向下滚动导航条滚出
  3. centos彻底删除mysql
  4. Storm入门学习随记
  5. java的基本数据类型特征
  6. .NET开源工作流RoadFlow-流程设计-流程步骤设置-策略设置
  7. 创建自己的oracle解释计划
  8. Android--启动系统的剪切图像功能并返回结果
  9. IBM MQ扩大队列最大消息长度
  10. iOS 初级数据持久化