Inheritance in C: good, bad, or else?

We have a large C code base developed over several decades. One of the features of the code is its great dependence on function pointers and pseudo-inheritance. The idiom (as discussed here ) is as follows:

typedef struct twod_ { double x, y; } twod; typedef struct threed_ { twod super; double z; } threed; threed *point_3d; twod *point_2d = (twod *)point3d; 

At what point point_2d->x and point_3d->x are the same memory block.

My questions:

  • Is this idiom popular in modern manufacturing code? (Any recommended open source examples)
  • This code requires performance - does this idiom help speed and / or memory usage?
  • The way it was implemented (or due to years of bloating code) now looks a bit like spaghetti code - generally speaking, is it a problem with an implementation or an idiom? Or, to put it another way, in an ideal world would 500,000 LOCs be quickly understood with this idiom?

Of course, saying "if it does not break, do not fix it" would be nice to keep in mind; however, this does not really help us at the moment, so we think that we may have to reorganize more deeply ...

Thanks!

+4
source share
4 answers

I would say that everything is in order, although, of course, it is easy to confuse, because it becomes quite detailed.

For me, the flagship open source implementation of "C with classes" is probably GTK + , specifically gobject .

+2
source

This will work, but the problem you should keep in mind is that the compiler will not warn / tell you anything about problems related to accessing a variable or something like that. This method is not type safe because sometimes these explicit drops can be evil. Take this example as an example:

 typedef struct _A { double x, y; } A; typedef struct _B { A super; double z; } B; typedef struct _C { A super; // we wanted B, but wrote A by mistake double w; } C; C* c; B* b = (B*)c; // will write to C::w instead of B::z and no one will warn you about this. // you'll need to track this by hand after your application crashes. b->z = 1234; 

This type of thing can easily land a plane on the ground. :)

On performance, it will end up the same as if you take the pointer to super manually (more safely than casting):

 A* a; B* b = &a->b; 

Also, I do not think that this is popular these days (at least not for young programmers like me who have developed programming in more modern compilers)

Also, is the C ++ compiler an option? More modern compilers these days, allowing structure inheritance, are better than casting in this way.

+1
source

Like (almost) everything in C, as long as you know what you are doing, everything is fine. But keep in mind that problems can occur very easily - for example, if you have a threed array, you cannot pass it to the twod array. (access to the members of this cast array is undefined behavior)

0
source

Is this idiom popular in modern production code? (Any recommended open source examples)

This is somewhat rare, although you see it from time to time. I would not call it popular, the use of polymorphism in C programs is often not enough.

This code requires performance - does this idiom help speed and / or memory usage?

This will not make the code more efficient than a structure containing all 3 members. In the worst case, legacy will make the code less efficient. The best way to tell is to parse the compiler for your specific platform and see for yourself.

... is this a problem with an implementation or an idiom?

Inheritance should generally be used carefully; inheritance for the sake of inheritance fills the goals. This is true for all OO languages.

Personally, I think this particular idiom and similar attempts to include useful OO features in C are problematic. Mainly because they add significant complexity to the program. Such things were preached in the book Object-Oriented Programming Using ANSI-C (Axel-Tobias Schreiner, released in the early 90's). I personally find that most of the things in this book are pretty awful.

in an ideal world, 500,000 LOC with this idiom would be quickly understood?

This is much more dependent on program structure, file structure, and coding standards than on this small OO idiom. Although, of course, 500k loc will not be quickly understood, even if the program is up to date.

0
source

Source: https://habr.com/ru/post/1391876/


All Articles