Scroll Down to See All▾
abortabsacosasctimeasctime_rasinassertatanatan2atexitatofatoiatolbsearchbtowccalloccatclosecatgetscatopenceilclearerrclockcoscoshctimectime_rdifftimediverferfcexitexpfabsfclosefdopenfeofferrorfflushfgetcfgetposfgetsfgetwcfgetwsfopenfprintffputcfputwsfreadfreefreopenfrexpfscanffseekfsetposftellfwidefwprintffwritefwscanfgetcgetchargetenvgetwcgetwchargmtimegmtime_rhypotisalnumisalphaisasciiisblankiscntrlisdigitisgraphislowerisprintispunctisspaceisupperiswalnumiswalphaiswblankiswcntrliswctypeiswdigitiswgraphiswloweriswprintiswpunctiswspaceiswupperiswxdigitisxdigitj0j1jnlabsldexpldivlocaleconvlocaltimelocaltime_rloglog10longjmpmblenmbrlenmbrtowcmbsinitmbsrtowcsmbstowcsmbtowcmemchrmemcmpmemcpymemmovememsetmktimemodfnextafternextafterlnexttowardnexttowardlnl_langinfoperrorpowprintfputcputcharputenvputsputwcputwcharqsortquantexpd32quantexpd64quantexpd128quantized32quantized64quantized128samequantumd32raiserandrand_rreallocregcompregerrorregexecregfreeremoverenamerewindscanfsetbufsetjmpsetlocalesetvbufsignalsinsinhsnprintfsprintfsqrtsrandsscanfstrcasecmpstrcatstrchrstrcmpstrcollstrcpystrcspnstrerrorstrfmonstrftimestrlenstrncasecmpstrncatstrncmpstrncpystrpbrkstrptimestrrchrstrspnstrstrstrtodstrtod32strtod64strtod128strtofstrtokstrtok_rstrtolstrtoldstrtoulstrxfrmswprintfswscanfsystemtantanhtimetime64tmpfiletmpnamtoasciitolowertouppertowctranstowlowertowupperungetcungetwcva_argva_copyva_endva_startvfprintfvfscanfvfwprintfvfwscanfvprintfvscanfvsprintfvsnprintfvsscanfvswprintfvswscanfvwprintfvwscanfwcrtombwcscatwcschrwcscmpwcscollwcscpywcscpywcsftimewcslenwcsncatwcsncmpwcsncpywcspbrkwcsptimewcsrchrwcsrtombswcsspnwcsstrwcstodwcstod32wcstod64wcstod128wcstofwcstokwcstolwcstoldwcstombswcstoulwcsxfrmwctobwctombwctranswctypewcwidthwmemchrwmemcmpwmemcpywmemmovewmemsetwprintfwscanfy0y1ynFunction Details : malloc
void *malloc(size_t size) ;
Return Type : void *
New Generic pointer that can point to any data type
Read about return values of malloc function .
1st Parameter Type : size_t
Platform-specific unsigned type for array indices and memory sizes.
1st Parameter
The number of bytes to allocate from the heap.
Read more about parameters of malloc in parameters section
The mallocfunction in C language dynamically allocates a specified amount of memory and returns a pointer to it.
It's action is similar to that of calloc function with one crucial difference: the allocated memory is uninitialized, meaning it may contain garbage values until explicitly initialized.
The memory block allocated by malloc must be composed of sequential memory units (bytes) in the heap.This allocation happens during runtime.
If available block of requested size is found malloc returns a pointer to the first byte of this block.
In case no free memory block of the specified size found in the heap malloc will return NULL (read more in return section).
Proper use of malloc requires freeing the memory with free function to prevent memory leaks.
Using malloc allows dynamic memory allocation, enabling programs to request memory at runtime when the exact size requirements are not known during compilation. This is essential for managing resources in scenarios like user input, data structures (e.g., linked lists or trees), or when working with variable-sized datasets.
Unlike static memory allocation, where memory size is fixed at compile time (e.g., arrays), or stack allocation, which is limited to the function scope and stack size, malloc provides flexibility by allocating memory on the heap. This memory persists until explicitly freed, making malloc suitable for managing larger, long-lived, or complex data structures that outlive their function's scope.
The memory block allocated by malloc must be composed of sequential memory units (bytes) in the heap.This allocation happens during runtime.
If available block of requested size is found malloc returns a pointer to the first byte of this block.
In case no free memory block of the specified size found in the heap malloc will return NULL (read more in return section).
Proper use of malloc requires freeing the memory with free function to prevent memory leaks.
Using malloc allows dynamic memory allocation, enabling programs to request memory at runtime when the exact size requirements are not known during compilation. This is essential for managing resources in scenarios like user input, data structures (e.g., linked lists or trees), or when working with variable-sized datasets.
Unlike static memory allocation, where memory size is fixed at compile time (e.g., arrays), or stack allocation, which is limited to the function scope and stack size, malloc provides flexibility by allocating memory on the heap. This memory persists until explicitly freed, making malloc suitable for managing larger, long-lived, or complex data structures that outlive their function's scope.
To Summarize the Workflow:
- 1.Checks the size parameter for validity
- 2.Handles size == 0 with implementation-defined behavior
- 3.Determines if sufficient memory is available in the heap
- 4.Requests additional memory from the operating system if needed
- 5.Searches the heap for a suitable block of free memory
- 6.Updates internal memory management data structures
- 7.Aligns the memory block to system requirements
- 8.Marks the memory block as allocated
- 9.Performs a final validation check before returning
- 10.Returns a pointer to the allocated memory or NULL if allocation fails
Read more about return type and value ofmalloc function in return section.
The mallocfunction takes 1
parameter:
PARAMETER FAILURES:
Invalid Size Values
Size Parameter Overflow Cases
Alignment Requirements
Platform-Specific Limits
- •size_t `size`: The number of bytes to allocate from the heap.
PARAMETER FAILURES:
Invalid Size Values
Size Parameter Overflow Cases
Alignment Requirements
Platform-Specific Limits
The malloc function return value :
- Returns a pointer to the allocated memory, or NULL if the request fails
malloc returns void* for several reasons, most important of which is type flexibility: void* is a generic pointer type that can be converted to any other pointer type
This allows malloc to allocate memory for any data type and there is no need to create separate malloc functions for each type
SUCCESS OUTCOME:
Returns: valid void* pointer to allocated memory block
Memory block is properly aligned and marked as allocated
Size requested was successfully accommodated
Block metadata is updated in heap management structures
FAILURE OUTCOME (Returns NULL):
Implementation Limits:
Heap arena full
Maximum number of allocations reached
Memory alignment requirements can't be met
Read more im parameters section about parameters related causes to failure
Memory Fragmentation Issues:
External Fragmentation: Enough total free memory exists, but split into small chunks
No single contiguous block large enough for request
Common in long-running programs with many allocations/deallocations
System Resource Limitations:
Process hitting memory limits set by OS
System running out of physical + swap memory
Process hitting ulimit restrictions
Size-Related Failures:
Size too large (approaching or exceeding SIZE_MAX)
Size causing integer overflow in internal calculations
Output
This example demonstrates basic usage of malloc to allocate memory for an array of integers.