Fri Apr 1 14:28:39 EDT 2011
Abstract data types vs. static RAM allocation
There is a clear conflict of interest between
1. C structures with static allocation
2. Data abstraction
It would be handy to be able to know the size of a data structure at
compile time, but hide its layout such that data accessors can be
I'm not so much interested in hiding reads, but writes really do need
special attention, as they can unknowingly introduce the worst kind of
(single-threaded) bug the data structure invariant violation.
I was thinking about a hack that would use the C "const" to protect a
struct from direct write access, but cast the const in the
implementation. That might not interfere well with optimization
though.. However it might be so that the C compiler doesn't use the
"const" for optimization simply because const pointers can be recast.
I'd like to pose this question on Stack Overflow. How to formulate?
Q: How to get at the size of a forwared declared struct?
A good practice when writing C code is to always use forward structure
declarations (incomplete types) in a header file, and keep the
implementation in a code file.
// in header
// in code
When writing C code in an embedded development setting with a lot of
memory perssure, it is often a good idea to statically allocate data
structures whenever possible. Doing so requires complete
types. i.e. a struct definition which has all fields accessible by the
Is there an elegant way to resolve the conflict of these two
constraints? Is it possible to provide struct size information at
compile time to make it possible to perform static allocation, but at
the same time hide the layout of the struct to prevent arbitrary