MSDN Documentation

malloc

The malloc function allocates memory and returns a pointer to the allocated space.

void* malloc(size_t size);

Parameters

Parameter Description
size Specifies the number of bytes to allocate.

Return Value

If the function succeeds, the return value is a pointer to the allocated memory. If the function fails to allocate the requested space, the return value is NULL.

Note: The allocated memory is not initialized. If you need initialized memory, consider using calloc.

Remarks

The malloc function allocates at least size bytes of memory. The value of size must be greater than zero.

The memory returned by malloc is guaranteed to be sufficiently aligned for any type of object.

To free allocated memory, use the free function.

Example

// Allocate memory for an integer int* ptr = (int*)malloc(sizeof(int)); if (ptr == NULL) { // Handle allocation failure perror( "malloc failed" ); return 1; } // Use the allocated memory *ptr = 10; printf( "Value: %d\n", *ptr ); // Free the allocated memory free(ptr);

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header stdlib.h
Library Use msvcrt.lib
DLL msvcrt.dll

See Also