So back in the last article, an object is the memory that is allocated by calling calloc(1,size) at the bottom of alloc, which corresponds to calloc, which corresponds to malloc so I was wondering what’s the difference between calloc and malloc? So let’s start today with a look at the difference between calloc and malloc!

1. The difference between malloc and calloc

Calloc (size_t __count, size_t __size) allocates a continuous count of size in the dynamic storage of memory. The function returns a pointer to the starting address of the allocation. (The initial value in this store is 0.) If the allocation was unsuccessful, NULL is returned

2. Malloc, function prototype malloc(size_t __size), allocates a continuous space of size in the dynamic storage area of memory. The return value of this function is the starting address of the allocation region (the initial value in this store is uncertain), or NULL if the allocation was unsuccessful

2. Explore the factors affecting the memory size of the object

In an object, only member variables, attributes, methods (object and class methods), protocol, classification, expansion of six factors may affect the size of the object memory, using the elimination method to explore the impact of the object memory size factors. Declare a class with no member variables, attributes, or methods, and get the memory size, personSize = 8

PersonSize = 8 personSize = 8 personSize = 8

PersonSize = 16; personSize = 16

4. Verify that member variables affect the memory size of the class. Add a _age member variable with personSize = 24

5. Add a protocol for the class to follow, personSize = 24, the protocol does not affect the object memory size

6. Create a ZFPerson class with personSize = 24

7. Add an extension and add a weight attribute, personSize = 24. The extension of the class does not affect the memory size of the object

Conclusion: Only the member variables of the class and its parent class affect the size of memory

3. Object memory byte alignment principle

In the previous article, we looked at the underlying alloc method for calculating object memory size, which is the instanceSize function

    inline size_t instanceSize(size_t extraBytes) const {
        if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
            return cache.fastInstanceSize(extraBytes);
        }

        size_t size = alignedInstanceSize() + extraBytes;
        // CF requires all objects be at least 16 bytes.
        if (size < 16) size = 16;
        return size;
    }
    uint32_t alignedInstanceSize() const {
        return word_align(unalignedInstanceSize());
    }
    static inline uint32_t word_align(uint32_t x) {
        return (x + WORD_MASK) & ~WORD_MASK;
    }
Copy the code

As you can see from the code, the object memory byte alignment core code is this return x + WORD_MASK) & ~WORD_MASK line, WORD_MASK is 7UL on 64-bit machines.

# define WORD_SHIFT 3UL # define WORD_MASK 7UL # define WORD_BITS 64 #else # define WORD_SHIFT 2UL # define WORD_MASK 3UL # define WORD_BITS 32 #endifCopy the code

(x + 7) & ~ 7:8 byte alignment

Conclusion: Objects are 8 byte aligned on 64-bit machines