-
Notifications
You must be signed in to change notification settings - Fork 0
Note
Random references/notes I think will be useful.
Definition (Urbanik). A "portable program" can be compiled and will run successfully on many different (a) compilers, (b) operating systems, and (c) hardware platforms with little or no change to the source code. (End of Definition)
- A. Dolenc, A. Lemmke, D. Keppel, Notes On Writing Portable Programs In C
- Stack Overflow thread
- C Portability from weird machines
From (one of these), I learned about perror
which appears to be the same as fprintf(stderr, "%s\n", strerror(errno))
(SO).
Strict aliasing rules denies us the ability to write:
struct base {
int id;
void (*calculate)(struct base *);
};
struct derived {
int id;
void (*calculate)(struct base *);
double inflation;
};
void do_thing(struct base *a)
{
a->calculate(a);
}
/* problem!!! */
void derived_calculate(struct derived *a)
{
struct base *b = (struct base*)a;
b->calculate(b);
}
The solution is to write:
struct derived {
struct base super;
double inflation;
};
Then casting (struct bar*)a
works as expected; one just needs to be careful about struct packing. This conforms to the C standard (as Bob Nystrom noted):
§ 6.7.2.1 13
Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.
Emphasis added.
Type inference boils down to two steps: (i) constraint generation, (ii) unification. Put slightly differently: setting up equations, then solving them.
- Joshua Dunfield, Neelakantan R. Krishnaswami Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism 13 pages, plus a small 78 page appendix
- Luca Cardelli's Basic Polymorphic Typechecking, 29 pages
- Eli Bendersky's Typechecking (blogpost)
- Danny Gratzer's An Explanation of Type Inference for ML/Haskell