2

The problem I'm facing in C is that I'd like to have a series of structs that have a base member from another struct. e.g.

struct foo {
    int a;
    void (*calculate)(struct foo *);
};
struct bar {
    int a;
    void (*calculate)(struct foo *);
    double b;
};
void do_thing(struct foo *a)
{
    a->calculate(a);
}

The problem I'm facing is that the following appears to violate strict aliasing rules.

void foo_calculate(struct foo *a)
{
    struct bar *b = (struct bar*)a;
}

The only way I've come up with to do this is to create a union inside struct foo that contains all the structs that inherit from it. Is there an easier way to accomplish this?

Bobby Sacamano
  • 540
  • 4
  • 15
  • Does the previously mentioned thread help you? – Kenny Meyer Jan 14 '16 at 23:56
  • @KennyMeyer I have no idea how I haven't seen that question before. That was almost the exact phrase I googled. It doesn't seem to account for strict aliasing rules though. – Bobby Sacamano Jan 14 '16 at 23:59
  • 1
    what about this: http://stackoverflow.com/questions/27980925/easy-struct-inheritance-pseudo-polymorphism-vs-strict-aliasing – Kenny Meyer Jan 15 '16 at 00:01
  • also this: http://stackoverflow.com/questions/31477307/oo-polymorphism-in-c-aliasing-issues – Kenny Meyer Jan 15 '16 at 00:02
  • I just answered a similar question yesterday: http://stackoverflow.com/questions/34777451/is-it-possible-to-do-inheritance-from-an-abstract-base-struct-or-simulate-someth/34781775#34781775 – Craig Estey Jan 15 '16 at 00:02
  • @CraigEstey can you mark this question as a duplicate of that thread? – Kenny Meyer Jan 15 '16 at 00:03
  • @CraigEstey you got it – Kenny Meyer Jan 15 '16 at 00:06
  • @KennyMeyer done ... – Craig Estey Jan 15 '16 at 00:09
  • I don't understand how I'm supposed to be able to access the derived class members, from a virtual function, in a solutions like this one. https://stackoverflow.com/questions/34777451/is-it-possible-to-do-inheritance-from-an-abstract-base-struct-or-simulate-someth – Bobby Sacamano Jan 15 '16 at 00:11
  • 2
    In addition to the SO question you are looking at, for an excellent article/tutorial on incomplete datatypes, encapsulation, data-hiding, dynamic linkage/late binding and object oriented approaches to dynamic data-structures, see [**Object Oriented Programming in ANSI-C**](http://www.cs.rit.edu/~ats/books/ooc.pdf). While it is written at, and requires, a fairly in-depth knowledge of C, it is well worth the effort requierd to digest the material. It covers a number of topics not included in most C books or tutorials. – David C. Rankin Jan 15 '16 at 00:17
  • It may or may not violate strict aliasing rules, but your approach is similar to mine [in the link]. If the compiler is complaining, try casting to `void *` on the right hand side. There are other answers, but mine is what I've used [with success] for years. – Craig Estey Jan 15 '16 at 01:28
  • Don't use compilers which don't guarantee (and document) good behaviour. Funky optimisations based on close reading of the standard such as "strict aliasing rules" or "assume possible undefined behaviour is never triggered" is bad behaviour. For, e.g. GCC, that means you have to explicitly disable quite a lot of optimisations. – Ben Jan 16 '16 at 16:29

0 Answers0