C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了以下程序是否符合严格的C99计划?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
标题几乎说明了一切,但我会重申这个问题……

以下程序是否符合C99标准的“严格符合程序”?

#include <stdlib.h>
/* Removing any pre-exisTing macro deFinition,in case one should exist in the implementation.
 * Seems to be allowed under 7.1.3 para 3,as malloc does not begin with _X where X is any  capital letter.
 * And 7.1.4 para 1 explicitly permits #undef of such macros.
 */
#ifdef malloc    
#undef malloc     
#endif            

/* Macro substitution has no impact on the external name malloc
 * which remains accessible,e.g.,via "(malloC)(s)".  Such use of
 * macro substitution seems enabled by 7.1.4 para 1,but not specifically
 * mentioned otherwise.
 */
void * journalling_malloc(size_t sizE);
#define malloc(s)     ((journalling_malloC)(s))      

int main(void)
{
    return malloc(10) == NULL ? 1 : 0;     
    /* Just for the sake of expanding the 
     * macro one time,return as exit code
     * whether the alLOCATIOn was performed. 
     */
}

解决方法

@H_450_9@ 让我们来看看C99标准对它的看法:

见第7节7.1.3,§1:

当您包含stdlib.h时,@L_976_3@malloc保留用作宏名称.

但是7.1.4,§1允许在保留名称上使用#undef:

这使得可以重新定义malloc,根据7.1.3,§2导致未定义的行为:

为什么标准会制定此限制?因为标准库的其他函数可以在原始函数方面实现为类函数宏,所以隐藏声明可能会破坏这些其他函数.

实际上,只要你的malloc定义满足标准为库函数提供的所有规定,你就应该没问题,这可以通过将实际调用包装到malloc()来实现.

大佬总结

以上是大佬教程为你收集整理的以下程序是否符合严格的C99计划?全部内容,希望文章能够帮你解决以下程序是否符合严格的C99计划?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。