• Stars
    star
    619
  • Rank 72,032 (Top 2 %)
  • Language
    C
  • License
    MIT License
  • Created about 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A dynamic array implementation in C similar to the one found in standard C++

c-cpp-badge CodeQL

This is an implementation of a std::vector like growable array, but in plain C89 code. The result is a type safe, easy to use, dynamic array that has a familiar set of operations.

It works by using the same trick as many allocators, which is to slightly allocate more data than requested, and using that extra padding in the front as storage for meta-data. Thus any non-null vector looks like this in memory:

+-----------------+------+----------+---------+
| elem_destructor | size | capacity | data... |
+-----------------+------+----------+---------+
                                    ^
                                    | user's pointer

Where the user is given a pointer to first element of data. This way the code has trivial access to the necessary meta-data, but the user need not be concerned with these details. The total overhead is 2 * sizeof(size_t) + sizeof(void (*)(void *)) per vector.

To allow the code to be maximally generic, it is implemented as all macros, and is thus header only. Usage is simple:

/* if this is defined, then the vector will double in capacity each
 * time it runs out of space. if it is not defined, then the vector will
 * be conservative, and will have a capcity no larger than necessary.
 * having this defined will minimize how often realloc gets called.
 */
#define CVECTOR_LOGARITHMIC_GROWTH

#include "cvector.h"
#include <stdio.h>

int main(int argc, char *argv[]) {

	/* this is the variable that will store the array, you can have
	 * a vector of any type! For example, you may write float *v = NULL,
	 * and you'd have a vector of floats :-). NULL will have a size
	 * and capacity of 0. Additionally, vector_begin and vector_end will
	 * return NULL on a NULL vector. Alternatively, for clarity of writing
	 * you can use the cvector_vector_type macro to define a vector of a
	 * given type.
	 */
	cvector_vector_type(int) v = NULL;

	(void)argc;
	(void)argv;

	/* add some elements to the back */
	cvector_push_back(v, 10);
	cvector_push_back(v, 20);
	cvector_push_back(v, 30);
	cvector_push_back(v, 40);

	/* remove an element by specifying an array subscript */
	cvector_erase(v, 2);

	/* remove an element from the back */
	cvector_pop_back(v);

	/* print out some stats about the vector */
	printf("pointer : %p\n", (void *)v);
	printf("capacity: %lu\n", cvector_capacity(v));
	printf("size    : %lu\n", cvector_size(v));

	/* iterator over the vector using "iterator" style */
	if (v) {
		int *it;
		int i = 0;
		for (it = cvector_begin(v); it != cvector_end(v); ++it) {
			printf("v[%d] = %d\n", i, *it);
			++i;
		}
	}

	/* iterator over the vector standard indexing too! */
	if (v) {
		size_t i;
		for (i = 0; i < cvector_size(v); ++i) {
			printf("v[%lu] = %d\n", i, v[i]);
		}
	}

	/* well, we don't have destructors, so let's clean things up */
	cvector_free(v);

	return 0;
}

More Repositories

1

edb-debugger

edb is a cross-platform AArch32/x86/x86-64 debugger.
C++
2,559
star
2

cpp-utilities

Miscellaneous C++11 utility classes and functions
C++
402
star
3

nedit-ng

a Qt5 port of the NEdit using modern C++14
C++
93
star
4

qhexview

A Qt widget design to give a nice looking but traditional hex view
C++
62
star
5

cpp-json

A fast & modern C++17 JSON library
C++
52
star
6

pretendo

A multiplatform NES emulator
Assembly
44
star
7

os64

A minimal example of an x86_64 higher half kernel loaded at the -2GB mark
C
40
star
8

qjson4

A Qt4 library providing an API compatible with Qt5's JSON implementation
C++
33
star
9

quixey

A small C like scripting language with a few small novel features.
C++
27
star
10

libp0f

modernized and C99 compliant port of p0f (Passive OS fingerprinting)
C
20
star
11

cxx17_printf

C++
20
star
12

edisassm

c++ disassembly library
C++
13
star
13

cxx11_printf

An implementation of printf using c++11's variadic templates
C++
12
star
14

libunif

A library for creating and loading UNIF files
C
9
star
15

p0f-ng

C++
9
star
16

qgmailnotifier

A portable Qt4/Qt5 based GMail notifier
C++
5
star
17

utf-converter

A command line utility to convert between unicode encodings
C++
4
star
18

qt5-action-editor

A port of qq14-actioneditor from Qt Quarterly to Qt5
C++
4
star
19

libc

An implementation of libc, attempting to be compliant with C89, C99 and C11 standards
C
3
star
20

lucent

A light and simple PHP 5.4 framework
PHP
2
star
21

cpp-cmd

Library for adding a shell like command interpreter based on linenoise
C++
2
star
22

gentoo-overlay

Overlay For Gentoo
Shell
2
star
23

reader

A class that makes writing a parser programatically easy
C++
2
star
24

nedit-nm

C++
2
star
25

command-line-tools

A collection of simple command line tools that I've created
C++
1
star
26

argos-parser

A simple C++ program which parses the .csi file generated by Argos and provides a more readable output.
C++
1
star