Skip to content
Implementations of the C++ Standard Library in... C. Can creatures armed without wings engage in flight?
C Other
  1. C 99.7%
  2. Other 0.3%
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
include
scripts
test
.clang-format
.gitattributes
.gitignore
LICENSE
README.md

README.md

C-STL

GitHub CircleCI Codacy Badge

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

Implementations

<algorithm>

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.

<vector>

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"

declare_vector(int)

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

define_vector(int)

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.

Testing

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.

License

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

You can’t perform that action at this time.