liveBook · Manning

Malloc

malloc is a C standard-library function that allocates a specified number of bytes of uninitialized storage and returns a pointer to it or a null pointer if allocation fails.

How 2 Manning books explain it

Modern C, Third Edition

malloc is a C library function that allocates a specified number of bytes of uninitialized storage and returns a pointer to it or a null pointer on failure.

How it is used
Uses dynamic allocation for storage whose size or lifetime must adapt to changing data.
How deep it goes
Explains allocation failure and release rules through implementations, including the ownership complication introduced by realloc.
Examples
Illustrates allocation with string duplication and a dynamically sized circular buffer.
What you can do after
Apply malloc to allocate storage, check for failure, and arrange a matching free.

Read how Modern C, Third Edition explains malloc →

Modern C

malloc is the C standard library function that allocates a specified number of bytes of storage and returns a pointer to the storage or a null pointer on failure.

How it is used
Deploys malloc for storage whose size or lifetime must vary during execution.
How deep it goes
Explains byte sizing, void pointers, allocation pairing, and valid free arguments, but defers implementation internals.
Examples
Uses a vector of doubles sized for one element per living person and examines platform feasibility.
What you can do after
Apply malloc to allocate byte-sized storage and maintain its lifetime with a valid matching free.

Read how Modern C explains malloc →

Also covered in

What to know first

Related concepts

Questions about malloc

What is malloc?

malloc is a C standard-library function that allocates a specified number of bytes of uninitialized storage and returns a pointer to it or a null pointer if allocation fails.

Which Manning books explain malloc?

Modern C, Third Edition and Modern C.

What should I know before learning malloc?

Manning books that teach malloc usually introduce float64 and resource exhaustion first.

What is the difference between malloc and cudaMalloc?

malloc is a C standard-library function that allocates a specified number of bytes of uninitialized storage and returns a pointer to it or a null pointer if allocation fails. cudaMalloc is a CUDA runtime API function that allocates memory in device memory.

What kinds of malloc are there?

Kinds of malloc include aligned_alloc, calloc and realloc.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.