In assignments where I have been forced to use C for scientific computing (rather than say, C++, my default choice), I often come across the following pattern:
There's usually a set of data that is commonly needed by many functions. For example, in solving PDEs, I would need to know the grid points, length of each dimension, etc. This leads me to often combining them in a struct, e.g.
struct parameters{
unsigned int num_x;
double length_x;
// so forth
};
I then end up repeating myself in nearly every function: void f(struct parameters* p, ...)
. This wouldn't be so bad if it made sense for every function to have it as part of its interface, but it is not always the case, and I dislike the repetition anyway.
Are there any workarounds or useful design patterns to deal with this? Making a global p
would fix this, but justifying the use of a global when they are generally not recommended is difficult.
Aucun commentaire:
Enregistrer un commentaire