加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C标准要求新char []产生对齐内存的意图是什么?

发布时间:2020-12-16 05:04:48 所属栏目:百科 来源:网络整理
导读:根据C 03标准,5.3.4 / 10 For arrays of char and unsigned char,the difference between the result of the new-expression and the address returned by the allocation function shall be an integral multiple of the most stringent alignment requirem
根据C 03标准,5.3.4 / 10

For arrays of char and unsigned char,the difference between the result of the new-expression and the address returned by the allocation function shall be an integral multiple of the most stringent alignment requirement (3.9) of any object type whose size is no greater than the size of the array being created.

分配函数(operator new []())也需要为任何可能的类型分配内存.所以引用的部分有效地说新的char [sizeof(T)]应该为类型T产生正确对齐的内存.

这个要求的目的是什么?我可以调用operator new()(或operator new []()),它保证返回正确对齐的内存并具有对齐的内存.我的意思是无论如何新的char []会调用operator new()[]所以我可以自己调用它.

引用要求的目的是什么?为什么不直接调用operator new []()?

解决方法

我通常只是直接调用operator new,但标准的作者也决定允许使用新的char []作为获取原始内存的方法.因此对齐要求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读