I’m working on some embedded software where there is some static information about “products”. Since the information for a certain product never changes during execution I would like to initialize these data structures at compile time to save some space on the stack/heap.
I made a Product class for the data, intending to make a huge array of all the products in the system and then do lookups in this structure, but I haven’t figured out quite how to get it working. The arrays are giving me loads of trouble. Some psuedo code:
class Product {
int m_price;
int m_availability[]; // invalid, need to set a size
... etc
// Constructor grabbing values for all members
Product(int p, int a[], ...);
}
static const Product products[] =
{
Product(99, {52,30,63, 49}, ...), // invalid syntax
...
}
Is there a way to making something like this work? The only thing I can think of would be to organize by attribute and skip the whole Product object. I feel that would make the whole thing harder to understand and maintain though.
Does anyone have any suggestions on how I might best organize this kind of data?
Thank you.
An old school C style static array of structs sounds like a perfect match to your requirements. Initializes at compile time, zero runtime overhead, no use of stack or heap. It’s not a co-incidence that C is still a major player in the embedded world.
So (one recipe – plenty of scope to change the details of this);