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.
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.
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.
Also covered in
What to know first
- float64Float64 is a 64-bit floating-point numeric type, commonly represented by the IEEE 754 binary64 format.
- resource exhaustionResource exhaustion is a condition in which a program or system cannot obtain enough of a required resource to operate normally.
Related concepts
- cudaMallocalternative to · cudaMalloc is a CUDA runtime API function that allocates memory in device memory.
- freeuses · free is a C standard-library function that releases dynamically allocated storage previously obtained from a compatible allocation function.
- memory allocationused by · Memory allocation is the process of reserving or assigning computer memory to hold program data or objects.
- callocincludes · calloc is a C standard-library function that allocates contiguous storage for an array of elements and initializes all allocated bytes to zero.
- reallocincludes · realloc is a C standard-library function that changes the size of an allocated memory block while preserving its existing contents up to the applicable size.
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.

