Skip to content

Latest commit

 

History

History
68 lines (47 loc) · 2 KB

README.md

File metadata and controls

68 lines (47 loc) · 2 KB

CBHMemoryKit

release pod licence coverage

A safer and easy-to-use interface for managing and manipulating memory.

Use

CBHMemoryKit provides safer c functions for managing and manipulating memory.

Examples:

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

Why?

Overflows:

These functions fail if an overflow occurs.

Use-After-Free:

Use-after-free errors are common. CBHMemory_free() helps avoid them by setting the pointer to NULL for you.

Licence

CBHMemoryKit is available under the ISC license.