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

前端 未结 7 1649
南旧
南旧 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:34

    There is a slight distinction: Calloc can decide to zero-out the memory only as it is needed and then there's the advantage of knowing the size of the elements.

    I can't name any implementations doing this, but it was thought for this.

    As an example:

    One callocates 4GB of memory, but the system has only 2GB: it wouldn't make sense to write 2GBs of zero into the virtual memory, therefore the system could set a dirty-flag on this memory to zero it out as it gets loaded into memory.

提交回复
热议问题