https://github.com/chris-huxtable/cbhmemorykit
A safer and easy-to-use interface for managing and manipulating memory.
https://github.com/chris-huxtable/cbhmemorykit
memory objective-c objective-c-library
Last synced: about 1 month ago
JSON representation
A safer and easy-to-use interface for managing and manipulating memory.
- Host: GitHub
- URL: https://github.com/chris-huxtable/cbhmemorykit
- Owner: chris-huxtable
- License: isc
- Created: 2019-07-08T21:41:49.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-22T15:09:51.000Z (almost 6 years ago)
- Last Synced: 2025-03-30T04:13:27.597Z (about 1 month ago)
- Topics: memory, objective-c, objective-c-library
- Language: Objective-C
- Size: 19.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CBHMemoryKit
[](https://github.com/chris-huxtable/CBHMemoryKit/releases)
[](https://cocoapods.org/pods/CBHMemoryKit)
[](https://github.com/chris-huxtable/CBHMemoryKit/blob/master/LICENSE)
[](https://github.com/chris-huxtable/CBHMemoryKit)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`:
```c
NSUInteger *slice = CBHMemory_alloc(10, sizeof(NSUInteger));
```Allocating a slice of 10 `NSUIntegers` where each value is set to 0:
```c
NSUInteger *slice = CBHMemory_calloc(10, sizeof(NSUInteger));
```Reallocating a slice of 10 `NSUIntegers` to 20:
```c
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:
```c
NSUInteger *slice = CBHMemory_calloc(10, sizeof(NSUInteger));
slice = CBHMemory_recalloc(slice, 20, sizeof(NSUInteger));
```Swapping bytes:
```c
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:
```c
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](https://github.com/chris-huxtable/CBHMemoryKit/blob/master/LICENSE).