Giter Site home page Giter Site logo

smemory's Introduction

smemory

The smemory library provides smart pointer structures and functions for managing memory in a safe and efficient way in C.

Smart pointers are objects that store pointers to dynamically allocated (heap) objects. They behave much like built-in C pointers except that they automatically delete the object pointed to at the appropriate time.

Features

  • Unique pointers
  • Shared pointers
  • Memory pool

Download

Clone the repository

git clone https://www.github.com/JoaoAJMatos/smart_ptr.git

Build the library

cd smart_ptr
./build.sh

Install the library (optional)

cd build
make install

Usage

Suppose we have the following struct with the following functions:

typedef struct {
      int id;
      char *name;
      float price;
} product_t;

product_t *product_make(int id, char *name, float price) {
    product_t *product = malloc(sizeof(product_t));

    product->id = id;
    product->name = name;
    product->price = price;

    return product;
}

void product_destroy(void *product) {
    free(product);
}

Unique pointers

#include <stdio.h>
#include <smart_ptr/unique_ptr.h>

#include "product.h"

int main(void) {
      // Create a unique pointer
      unique_ptr *product = unique_ptr_make(product_make(1, "Product 1", 1.99), product_destroy);

      // Use unique_ptr_get to access the pointer's data
      printf("Product id: %d\n", unique_ptr_get(product)->id);

      // Move ownership to another unique pointer
      // The original pointer can no longer be used
      unique_ptr *product2 = unique_ptr_move(product);

      printf("Product id: %d\n", unique_ptr_get(product2)->id);
      printf("Product id: %d\n", unique_ptr_get(product)->id); // This segfaults

      // All memory is automatically dealocated once the smart pointers
      // go out of scope
}

Shared pointers

#include <stdio.h>
#include <smart_ptr/shared_ptr.h>

#include "product.h"

int main(void) {
      // Create a shared_ptr 
      shared_ptr *product = shared_ptr_make(product_make(1, "Product 1", 1.99), product_destroy);

      // Create a copy of the shared_ptr
      shared_ptr *product2 = shared_ptr_copy(product);

      // Use shared_ptr_get to access the pointer's data
      printf("Product id: %d\n", shared_ptr_get(product)->id);
      printf("Product id: %d\n", shared_ptr_get(product2)->id);

      // The data is still accessible through the copy
      printf("Product id: %d\n", shared_ptr_get(product2)->id);

      // All memory is automatically dealocated once the smart pointers
      // go out of scope
}

License

This project is licensed under the MIT License - see the LICENSE file for details

smemory's People

Contributors

joaoajmatos avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.