I have to design an abstract datatype, but I'm not allowed to use dynamic allocation. Seems a bit tricky...
What I currently have:
In adt.c:
struct adt
{
bool b;
};
const size_t adtSize = sizeof( struct adt );
// Initialisation of the adt
void Adt_New( struct adt* anAdt, bool b )
{
// No calloc allowed...
anAdt->b = b;
}
In adt.h, the ugly part comes:
struct adt; // adt structure
extern const size_t adtSize;
// Bear with me...
#define ADT_DATA( NAME ) uint8_t NAME ## _storage[ adtSize ]; \
memset( &NAME ## _storage , 0 , adtSize ); \
struct adt* NAME = (adt*) & NAME ## _storage;
Now I can use it like this:
void TestAdt()
{
ADT_DATA( a );
Adt_New( a, true );
}
On the pro side, I have an opaque data type and I don't have to use dynamic allocation.
Con the con side, this is just ugly. And when I try to call ADT_DATA( ... ) not from within a function (globally, e.g.), I get an error message.
Is it possible to improve this? Currently, my only alternative is to make the data type public...
TIA for your Ideas!