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

前端 未结 7 1634
南旧
南旧 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:37

    calloc(4, 6) and calloc(6, 4) are NOT the same:

    On a typical 32bit/64bit system, the first would allocate 32 bytes and the second 24 bytes.


    void *calloc(size_t nelem, size_t elsize);
    

    The key point is that calloc must return the memory as if it was correctly aligned as an array. It is meant to allocate an array and be used as follows:

    A *p = (A*)calloc(count, sizeof(A));
    for (int i = 0; i < count; ++i)
    {
        f(&(p[i]));
        // f(p + i) is also valid
    }
    

    or

    A *p = (A*)calloc(count, sizeof(A));
    for (A *q = p; q != p + count; ++q)
    {
        f(q);
    }
    

    calloc is supposed to allocate the array taking into account padding and other operating requirements of the target system. So on most 32bit machines, where a 6 byte structure would need to be padded to 8 bytes, it would allocate 4 lots of 8 bytes.

    calloc where the first argument is a sizeof() is most likely a bug and should be investigated.

    calloc where the second argument is not sizeof(atype) is undefined. It reeks of hidden assumptions and is dangerous to port.

    Clarification: On a typical 32bit/64bit system, a structure is likely to be padded and aligned to a multiple of 32bits. As such on these systems sizeof would not return 6 bytes. In fact there is no guarantee that the compiler would not pad and align to some multiple of 16 bytes if that is what the compiler/platform requires.

    My answer is based on the fact you should not make assumptions about structure size. They can change with compiler options, or target platform. Just make sure that your second argument is a sizeof expression and don't make assumptions.


    From the standard:

    The calloc() function shall allocate unused space for an array of nelem elements each of whose size in bytes is elsize. The space shall be initialized to all bits 0.

    The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned shall point to the start (lowest byte address) of the allocated space.

提交回复
热议问题