Is calloc(4, 6) the same as calloc(6, 4)?

前端 未结 7 1648
南旧
南旧 2020-12-08 06:49

I\'m a beginner C programmer, and I assumed that this would be the case, but would like some affirmation if possible.

If they are the same, why not just take one arg

7条回答
  •  误落风尘
    2020-12-08 07:27

    To the excellent responses posted, I want to add one more point of difference between using calloc(nelem, elsize) versus malloc(nelem * elsize): quality implementations of calloc will ensure that if your nelem and elsize were big enough to cause an integer overflow when multiplied together, it will fail rather than cause an undersized allocation, as a naive malloc invocation would.

    Just this feature alone would be enough for me to prefer calloc to malloc. Background reading.

提交回复
热议问题