当前位置:首页 > 公众号精选 > C语言编程
[导读]来自:知乎,作者:Name1e5s链接:https://zhuanlan.zhihu.com/p/40490357故事要从前两天交流群中一位同学提到的这个问题开始这个问题看起来十分刁钻,不过稍有常识的人都知道,制定C标准的那帮语言律师也不是吃白饭的,对这种奇奇怪怪的问题一定会有定...

来自:知乎,作者:Name1e5s

链接:https://zhuanlan.zhihu.com/p/40490357

故事要从前两天交流群中一位同学提到的这个问题开始这个问题看起来十分刁钻,不过稍有常识的人都知道,制定 C 标准的那帮语言律师也不是吃白饭的,对这种奇奇怪怪的问题一定会有定义。翻阅C17 标准 草案 N2176,在 

7.22.3 节里,有如下说法:The order and contiguity of storage allocated by successive calls to the aligned_alloc, calloc, malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated). The lifetime of an allocated object extends from the allocation until the deallocation. Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer is returned. If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned to indicate an error, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.在这里,标准委员会明确规定了:当 

malloc 接到的参数为 0 时,其行为是由实现定义的(implementation-defined)。由实现定义的行为这个词就提醒我们,在实际编程时如果要考虑到程序在多个运行环境下进行运行时,不能对 

malloc 返回的数值进行任何假设。换言之,没事儿不要吃饱了撑的在实际编程中写下 

malloc(0) 这种天怒人怨的代码。但是,这个无意义的问题吸引了我的兴趣。因此我开始查阅 

glibc 的源代码,依此了解在 

glibc 下,

mallloc(0) 的行为。在 

glibc2.27/malloc/malloc.c 中,有如下注释:/*
  malloc(size_t n)
  Returns a pointer to a newly allocated chunk of at least n bytes, or null
  if no space is available. Additionally, on failure, errno is
  set to ENOMEM on ANSI C systems.

  If n is zero, malloc returns a minumum-sized chunk. (The minimum
  size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
  systems.)  On most systems, size_t is an unsigned type, so calls
  with negative arguments are interpreted as requests for huge amounts
  of space, which will often fail. The maximum supported value of n
  differs across systems, but is in all cases less than the maximum
  representable value of a size_t.
*/
注释已经说的很清楚了,当我们执行 

malloc(0) 时,我们实际会拿到一个指向一小块内存的指针,这个指针指向的(分配给我们的)内存的大小是由机器决定的。细读代码,可以发现,将读入的内存大小进行转换是由宏 

checked_request2size 实现的。相关的宏定义如下:/* pad request bytes into a usable size -- internal version */
#define request2size(req)                                         \
  (((req)   SIZE_SZ   MALLOC_ALIGN_MASK 

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

——该收购旨在进一步扩大神经调控产品组合,为慢性腰疼患者提供更多治疗解决方案 马萨诸塞州马尔伯勒2023年9月20日 /美通社/ -- 波士顿科学公司(纽约证券交易所代码:BSX)宣布现已签订Relievant Med...

关键字: SYSTEMS 医疗技术 EV AN

上海2023年1月30日 /美通社/ -- 2022年8月5日,富士胶片(中国)投资有限公司面向摄影师及爱好者发起了"2022富士百人影创官"招募活动,提供百余台器材支持创作,与影创者共同探索富士胶片...

关键字: 富士 影像 SPACE 相机

该项完全托管的服务可实现复杂、动态的3D模拟仿真,可对具有100万个以上对象的系统进行建模,快速预测现实世界的结果,以及创建沉浸式的训练空间 Duality Robotics、Lockheed Martin和u...

关键字: SIM SPACE 亚马逊 模拟

为了支持快速商业增长和全球扩张,SimSpace正在扩大其高管团队,并通过在美国、欧洲、中东和非洲以及亚太地区开设更多办事处来增强他们的服务。 波士顿2022年11月17日...

关键字: SIM SPACE 网络安全

纽约2022年9月30日 /美通社/ -- 当地时间9月12日,海投全球创始人兼CEO Jerry王金龙受邀出席了在纽约举行的全球私募股权盛会SuperReturn,并在"私募信贷"板块中发表了名为&...

关键字: RETURN SUPER CE 创始人

华尔街的各大银行最近终于开始出售一大批并购交易相关债务,这些银行可能难以达成足够多的杠杆贷款交易。包括美国银行、瑞士信贷和高盛集团在内的华尔街投行在为对思杰系统公司(Citrix Systems)的杠杆收购交易提供的融资...

关键字: 华尔街 SYSTEMS CITRIX

旅行不设限 商务亦休闲 上海2022年9月21日 /美通社/ -- 近年来,"Bleisure" 休闲商务旅行的出行模式蔚为风潮,越来越多的宾客在商务出行时愈发关注休闲的需求。作为高端商务...

关键字: BSP 数字化 SPACE WORKS

GRL通过与FuturePlus的合作伙伴关系,扩大了全球七个实验室所提供的DDR和LPDDR内存测试服务组合  加利福尼亚州圣克拉拉市2022年9月15日 /美...

关键字: DDR FUTURE SYSTEMS 内存

上海2022年8月11日 /美通社/ -- 8月19日至8月30日,富士X-SPACE策划的两个摄影项目将亮相北京798艺术区悦·美术馆:玛格南摄影师Olivia Arthur(奥利维娅·亚瑟...

关键字: CONNECTION SPACE 富士 影像

纽约, Aug. 02, 2022 (GLOBE NEWSWIRE) -- 混合基础设施解决方案和托管服务领先提供商之一NYI通过增加原由Equinix拥有的空间,扩展其60 Hudson Street的运营场所。此次扩...

关键字: ST DSO SYSTEMS UI
关闭