If you want to go the calloc/free route, then it would be better to write a reusable library, e.g.,
Code:
#ifndef ZACARIAZ_BITSET_H
#define ZACARIAZ_BITSET_H
/* bitset.h */
#include <stdint.h>
typedef struct Bitset_ Bitset;
void BitsetInit(Bitset *bitset, uint_fast32_t n);
void BitsetDestroy(Bitset *bitset);
int_fast8_t BitsetSetBit(Bitset *bitset, uint_fast32_t n);
int_fast8_t BitsetResetBit(Bitset *bitset, uint_fast32_t n);
int_fast8_t BitsetGetBit(Bitset *bitset, uint_fast32_t n);
#endif
Code:
#include <stdlib.h>
#include "bitset.h"
struct Bitset_ {
uint_fast8_t *data;
uint_fast32_t limit;
};
static const uint_fast8_t BitsetTypeWidth = sizeof(uint_fast8_t) * CHAR_BIT;
void BitsetInit(Bitset *bitset, uint_fast32_t n) {
bitset->limit = ((n % BitsetTypeWidth) ? n : (n + 1)) * BitsetTypeWidth;
bitset->data = calloc(bitset->limit, sizeof(bitset->data[0]));
}
void BitsetDestroy(Bitset *bitset) {
free(bitset->data);
bitset->data = NULL;
bitset->limit = 0;
}
/* ... */