malloc(3) – a quick summary

So, let's summarize the key points regarding usage of the malloc(3) API:

  • malloc(3) dynamically (at runtime) allocates memory from the process heap
    • As we shall soon learn, this is not always the case
  • The single parameter to malloc(3) is an unsigned integer value—the number of bytes to allocate
  • The return value is a pointer to the start of the newly allocated memory chunk on success, or NULL on failure:
    • You must check for the failure case; don't just assume it will succeed
    • malloc(3) always returns a memory region that is aligned on an 8-byte boundary
  • The content of the newly allocated memory region is considered to be random
    • You must initialize it before reading from any part of it
  • You must free the memory ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.