twitter
    Find out what I'm doing, Follow Me :)

Back To The "Malloc" Function

Hopefully the above description of the "heap" and the overall plan for dynamic allocation helped you to understand what we are doing with the "malloc" function. It simply asks the system for a block of memory of the size specified, and gets the block with the pointer pointing to the first element of the block.

The only argument in the parentheses is the size of the block desired and in our present case, we desire a block that will hold one of the structures we defined at the beginning of the program. The "sizeof" is a new function, new to us at least, that returns the size in bytes of the argument within its parentheses. It therefore, returns the size of the structure named animal, in bytes, and that number is sent to the system with the "malloc" call. At the completion of that call, we have a block on the heap allocated to us, with pet1 pointing to the first byte of the block.