https://github.com/ho11ow1/u_ptr
A Smart Pointer Implementation in C inspired by 'std::unique_ptr' from C++
https://github.com/ho11ow1/u_ptr
Last synced: 2 months ago
JSON representation
A Smart Pointer Implementation in C inspired by 'std::unique_ptr' from C++
- Host: GitHub
- URL: https://github.com/ho11ow1/u_ptr
- Owner: Ho11ow1
- License: mit
- Created: 2024-12-14T14:48:22.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2025-03-21T15:38:50.000Z (3 months ago)
- Last Synced: 2025-03-21T16:45:05.612Z (3 months ago)
- Language: C
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# u_ptr

A lightweight implementation of a unique pointer in C, providing automatic memory management and type-safe operations.## Features
- Automatic memory management
- Type-safe operations
- Support for basic data types (char, int, float, double, void*)
- Simple API for pointer manipulation
- Print functionality with automatic type detection## API Reference
### u_ptr_init
Initializes a new unique pointer with the given data.
```c
u_ptr* u_ptr_init(void* ptr, size_t size, dataType type);
```### u_ptr_get
Retrieves the data stored in the unique pointer.
```c
void* u_ptr_get(u_ptr* ptr);
```### u_ptr_print
Prints the data stored in the unique pointer with automatic type detection.
```c
void u_ptr_print(u_ptr* ptr);
```### u_ptr_reset
Resets the unique pointer to NULL and sets the Size to 0
```c
void* u_ptr_reset(u_ptr* ptr);
```### u_ptr_destroy
Completely deallocates the pointer and its data.
```c
void u_ptr_destroy(u_ptr** ptr);
```## Usage example
```c
int i_val = 123;
u_ptr* i_ptr = u_ptr_init(&i_val, sizeof(i_val), INT);
u_ptr_print(i_ptr); // Prints: 123u_ptr_reset(i_ptr);
i_val = 456;
i_ptr = u_ptr_init(&i_val, sizeof(i_val), INT); // re-initialise to change value
u_ptr_print(i_ptr); // Prints: 456u_ptr_destroy(&i_ptr);
```## Building
The project can be built using any standard C compiler:
```bash
gcc main.c src/u_ptr.c -o u_ptr
```
```bash
# Using the Visual Studio Native tools
cl main.c src/u_ptr.c /I.
```## Todo
- [ ] Implement move semantics
- [ ] Add array support