最近遇到了一个诡异的问题, 数组的数据不对, 最后发现是两个类型的大小不一样导致的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class alloc
{
public:
void* operator new(size_t n){...}
void operator delete(void* p) {...}
};
  
class alloc2
{
public:
void* operator new(size_t n){...}
void operator delete(void* p) {...}
};
  
class DualQuaterion : public class alloc
{...};
  
//override base with different allocation strategy
class BoneDQ : public dualquaterion, public class alloc2
{
public:
using alloc2::operator new;
using alloc2::operator delete;
};

两个地方使用的数组类型不一样, 导致数据错误.也就是sizeof(BoneDQ) != sizeof(DualQuaternion).

这是VS2012下的结果, 换用GCC, 两个类型大小一样.

这个真的没有想到.

最后查到问题在这里:

http://en.cppreference.com/w/cpp/language/ebo

上面说的基本限制: 如果同时有连续两个一样的子对象, 那么这两个对象地址不一样(C++标准规定), 导致empty base被阻止.

但跟我遇到的问题仍然不一样.

但是下面又提到,

1
2
Empty base optimization is required for StandardLayoutTypes in order to maintain the requirement that the pointer to a standard-layout object, converted using reinterpret_cast, points to its initial member, which is why one of the requirements for a standard layout type is "has no base classes with non-static data members and has no base classes of the same type as the first non-static data member".
(since C++11)

也就是说, 只有C++11以后才要求empty base optimization是必须的. 所以VS2012的处理也是正确的.

最新文章

  1. ajax获取json数据 for select2
  2. Latex中插入C语言代码
  3. 自己写一个JS单向数据流动库----one way binding
  4. SqlServer类库(自定义)
  5. 用Map-Reduce的思维处理数据
  6. gulp + webpack 构建多页面前端项目
  7. OSGMFC
  8. linux和Windows下文本格式转换
  9. AutoLayout UITableViewCell 动态高度
  10. bzoj1954 poj3764
  11. Raid1源代码分析--同步流程
  12. 基于visual Studio2013解决C语言竞赛题之前言
  13. PHP socket类
  14. selenium 多线程
  15. [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience
  16. SpringMVC基础配置(通过注解配置,非xml配置)
  17. CSS设置边框、符号、背景样式、链接属性
  18. kafka读书笔记《kafka权威指南》2018
  19. PHP stripos()、strripos()和strrpos() 使用方法和区别
  20. 网络编程——The C10K Problem(C10K = connection 10 kilo 问题)。k 表示 kilo,即 1000

热门文章

  1. 兼容ie7的导航下拉菜单
  2. PHP生成二维码库phpqrcode
  3. PHP函数:生成N个不重复的随机数
  4. FileUpload控件「批次上传 / 多档案同时上传」的范例--以「流水号」产生「变量名称」
  5. C# 截取带路径的文件名字,扩展名,等等 的几种方法
  6. 十天学会单片机Day4串行口通信
  7. Python学习教程(learning Python)--2.3 Python自定义函数传参函数设计
  8. Qualcomm device使用ION memory manager取代PMEM
  9. 我的WPF控件库——KAN.WPF.XCtrl(141105)
  10. ED/EP系列2《文件结构》