libft
Loading...
Searching...
No Matches
Files | Functions
Memory Management

Functions for low-level memory operations. More...

Files

file  ft_memory.c
 Low-level memory manipulation utilities.
 
file  ft_memory.h
 Low-level memory manipulation functions.
 

Functions

void ft_bzero (void *s, size_t n)
 Sets a block of memory to zero.
 
void * ft_calloc (size_t nmemb, size_t size)
 Allocates and zeroes memory for an array.
 
void * ft_memchr (const void *s, int c, size_t n)
 Scans memory for a specific byte.
 
int ft_memcmp (const void *s1, const void *s2, size_t n)
 Compares two memory blocks.
 
void * ft_memcpy (void *dest, const void *src, size_t n)
 Copies memory from source to destination.
 
void * ft_memmove (void *dest, const void *src, size_t n)
 Copies memory safely, handling overlapping regions.
 
void * ft_memset (void *s, int c, size_t n)
 Fills memory with a constant byte.
 
void * ft_realloc (void *ptr, size_t old_size, size_t new_size)
 Resizes a memory block.
 

Detailed Description

Functions for low-level memory operations.

This group contains functions that replicate or extend common C standard library memory operations, including:

These utilities provide safer or more convenient alternatives to their standard counterparts and are useful throughout custom C projects.

This group includes:

Function Documentation

◆ ft_bzero()

void ft_bzero ( void *  s,
size_t  n 
)

#include <include/ft_memory.h>

Sets a block of memory to zero.

Sets the first n bytes of the memory area pointed to by s to zero ('\0' bytes). Useful for initializing buffers or memory blocks.

Parameters
sPointer to the memory area to zero out.
nNumber of bytes to set to zero.
Returns
Nothing.
Note
Behaves similarly to the standard bzero function. Often used with malloc, calloc, or custom memory allocators.
See also
ft_memset

◆ ft_calloc()

void * ft_calloc ( size_t  nmemb,
size_t  size 
)

#include <include/ft_memory.h>

Allocates and zeroes memory for an array.

Allocates memory for nmemb elements of size bytes each and initializes all memory to zero. Includes overflow protection.

Parameters
nmembNumber of elements.
sizeSize of each element in bytes.
Returns
Pointer to the allocated memory, or NULL on failure.
Note
Equivalent to malloc(nmemb * size) followed by ft_memset(ptr, 0, ...).
See also
ft_memset

◆ ft_memchr()

void * ft_memchr ( const void *  s,
int  c,
size_t  n 
)

#include <include/ft_memory.h>

Scans memory for a specific byte.

Searches the first n bytes of the memory area pointed to by s for the first occurrence of the byte c.

Parameters
sPointer to the memory block to scan.
cByte to search for.
nNumber of bytes to scan.
Returns
A pointer to the matching byte, or NULL if not found.

◆ ft_memcmp()

int ft_memcmp ( const void *  s1,
const void *  s2,
size_t  n 
)

#include <include/ft_memory.h>

Compares two memory blocks.

Compares the first n bytes of memory areas s1 and s2. Comparison is done byte by byte using unsigned characters.

Parameters
s1Pointer to the first memory block.
s2Pointer to the second memory block.
nNumber of bytes to compare.
Returns
Difference between first differing bytes, or 0 if equal.

◆ ft_memcpy()

void * ft_memcpy ( void *  dest,
const void *  src,
size_t  n 
)

#include <include/ft_memory.h>

Copies memory from source to destination.

Copies n bytes from src to dest. Performs fast 8-byte chunks when possible. Does not support overlapping memory regions.

Parameters
destPointer to destination memory.
srcPointer to source memory.
nNumber of bytes to copy.
Returns
A pointer to dest, or NULL if either pointer is NULL.
Warning
Use ft_memmove instead when memory regions overlap.

◆ ft_memmove()

void * ft_memmove ( void *  dest,
const void *  src,
size_t  n 
)

#include <include/ft_memory.h>

Copies memory safely, handling overlapping regions.

Copies n bytes from src to dest. Chooses forward or backward copy direction based on pointer alignment to avoid corruption.

Parameters
destPointer to destination memory.
srcPointer to source memory.
nNumber of bytes to copy.
Returns
A pointer to dest.
Note
This function is safe for overlapping memory regions.
See also
ft_memcpy

◆ ft_memset()

void * ft_memset ( void *  s,
int  c,
size_t  n 
)

#include <include/ft_memory.h>

Fills memory with a constant byte.

Sets the first n bytes of the memory area pointed to by s to the byte value c, converted to unsigned char.

Parameters
sPointer to the memory area to fill.
cByte value to write into memory.
nNumber of bytes to write.
Returns
A pointer to the memory area s.

◆ ft_realloc()

void * ft_realloc ( void *  ptr,
size_t  old_size,
size_t  new_size 
)

#include <include/ft_memory.h>

Resizes a memory block.

Allocates new memory of size new_size, copies up to old_size bytes from ptr into the new block, and frees the old memory.

Parameters
ptrPointer to previously allocated memory.
old_sizeSize of the old block.
new_sizeDesired new size.
Returns
Pointer to new memory block, or NULL on failure.
Note
If ptr is NULL, behaves like malloc(new_size). If new_size is 0, frees memory and returns NULL.
See also
ft_memmove