A safer and easy-to-use interface for managing and manipulating memory.
CBHMemoryKit
provides safer c functions for managing and manipulating memory.
Allocating a slice of 10 NSUIntegers
:
NSUInteger *slice = CBHMemory_alloc(10, sizeof(NSUInteger));
Allocating a slice of 10 NSUIntegers
where each value is set to 0:
NSUInteger *slice = CBHMemory_calloc(10, sizeof(NSUInteger));
Reallocating a slice of 10 NSUIntegers
to 20:
NSUInteger *slice = CBHMemory_alloc(10, sizeof(NSUInteger));
slice = CBHMemory_realloc(slice, 20, sizeof(NSUInteger));
Reallocating a slice of 10 NSUIntegers
to 20 where each new value is set to 0:
NSUInteger *slice = CBHMemory_calloc(10, sizeof(NSUInteger));
slice = CBHMemory_recalloc(slice, 20, sizeof(NSUInteger));
Swapping bytes:
NSUInteger *slice = {4, 5, 6, 7, 0, 1, 2, 3};
CBHMemory_swapBytes(slice[0], slice[4], 4, sizeof(NSUInteger));
// slice = {0, 1, 2, 3, 4, 5, 6, 7}
Freeing heap allocated memory:
NSUInteger *slice = CBHMemory_calloc(10, sizeof(NSUInteger));
// Do some work...
CBHMemory_free(slice);
// slice = NULL
These functions fail if an overflow occurs.
Use-after-free errors are common. CBHMemory_free()
helps avoid them by setting the pointer to NULL for you.
CBHMemoryKit is available under the ISC license.