Implementations of the C++ Standard Library in... C. Can creatures armed without wings engage in flight?
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Luiserebii 61a5717b78
Making (void) function parameter explicit
2 weeks ago
.circleci Making executable, and adding valgrind to programs to install via CircleCI 1 month ago
include Making (void) function parameter explicit 2 weeks ago
scripts Accepting linting changes, opening up lint, and finally, now to push 1 month ago
test Alright, test-vector-struct-point now passing 3 weeks ago
.clang-format Adding .clang-format and 1 month ago
.gitattributes Added valgrind for struct vector to Makefile, and adding .gitattributes to correct identification of .h as C 1 month ago
.gitignore Initial commit 1 month ago
LICENSE Adding LICENSE and updating 1 month ago Updating snippets with license 1 month ago


GitHub CircleCI Codacy Badge

Implementation of the C++ Standard Library in C. Can creatures armed without wings engage in flight?



Functions from the <algorithm> header are currently being implemented as macros, often requiring an explicit passing of the type being used. For more, check out algorithm.h.


Emulating the C++ <vector> in C is difficult, but macros allow us some room to play. Two macros, declare_vector and define_vector, allow us to explicitly provide a type (much like a template) and construct the rest according to our needs.

Since the declaration of the vector struct data type and associated functions should ideally belong in a header file, it is advised to use the declare_vector within one, and use define_vector in a matching .c file. For example, to create an int vector, one might write:

/** vector_int.h **/
#ifndef VECTOR_INT_H
#define VECTOR_INT_H

#include "vector.h"


/** vector_int.c **/
#include "vector_int.h"
#include "vector.h"


This will then expand into the appropriate vector struct (e.g. vector_int) with matching functions. All functions are appended with the type, with the general format vector_FUNCTION_TYPE. Therefore, to push_back on an int vector, one would call vector_push_back_int.

As vector relies on dynamically-allocated memory via malloc, a convenience function vector_free_TYPE has been provided to release a vector after it has finished being used.

Further documentation on the currently implemented functions can be found in vector.h.


The test/ directory contains tests which intend to exercise typical usage. To run all tests, simply use the command make test, which also includes diagnostic information from valgrind to ensure resources are being freed properly. The simple test framework Unity is currently being used to write them.


This code has been licensed under the GNU General Public License v3.0.