双字段的calloc()是否始终求值为0.0? [英] Does calloc() of a double field always evaluate to 0.0?

查看:67
本文介绍了双字段的calloc()是否始终求值为0.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 双字段的 calloc()是否总是评估为 0.0 ?
  • Does calloc() of a double field always evaluate to 0.0?

此外:

  • float 字段的 calloc()是否总是评估为 0.0f ?
  • int unsigned int 字段的 calloc()是否始终取值为 0 ?
  • >
  • Does calloc() of a float field always evaluate to 0.0f?
  • Does calloc() of an int or unsigned int field always evaluate to 0?

,下面的 assert()是否会在所有平台上始终成功?

That is, will the assert() below always succeed on all platforms?

double* d = calloc(1, sizeof(double));
assert(*d == 0.0);
free(d);

推荐答案

calloc 将已分配内存的所有字节设置为零.

The calloc sets all bytes of the allocated memory to zero.

碰巧的是,这也是有效的 IEEE754 (这是最常见的浮动格式计算机上的点值)表示 0.0 .

As it happens, that's also the valid IEEE754 (which is the most common format for floating point values on computers) representation for 0.0.

IIRC没有C规范的任何部分要求实现使用IEEE754,因此请注意,它不是可移植的.但是实际上是这样(如果您要在非IEEE754系统上工作,那么您应该已经积累了足够的经验,已经知道了这一点以及如何解决此类问题).

IIRC there's no part of the C specification that requires an implementation to use IEEE754, so to be picky it's not portable. In reality though, it is (and if you're ever going to work on a non-IEEE754 system then you should have gathered enough experience to already know this and how to solve such problems).

还请注意,这对指针也有效.在您可能接触的所有系统上,空指针应等于 0 .但是可能有些系统没有使用它,但是如果您在这样的系统上工作,则应该已经知道(如果使用 NULL ,那应该不是问题).

Also note that this also is valid for pointers. On all systems you're likely to come in contact with, a null pointer should be equal to 0. But there might be systems where it isn't, but if you work on such systems you should already know about it (and if you use NULL then it should not be a problem).

这篇关于双字段的calloc()是否始终求值为0.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆