Lies in education [was Re: The "loop and a half"]

Marko Rauhamaa marko at pacujo.net
Wed Oct 11 14:35:53 EDT 2017


Chris Angelico <rosuav at gmail.com>:

> On Thu, Oct 12, 2017 at 2:43 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> That is not immediately all that significant but points to subtle
>> incompatibilities between the data models of C and C++.
>
> Indeed - their handling of empty structs is different. But that
> doesn't invalidate my point; how often do you declare a variable that
> has nothing in it?

In my case, this particular problem caused a bug once some years back.

> Remember, a Python object with no attributes still
> has an identity, a type, and so on; in C, data has no type or
> identity, so this truly has no data attributes. This is legal but
> extremely esoteric, and the difference in sizeof is less about an
> incompatible data model and more about the definition of an empty
> struct.

Here's a tiny example from the Linux kernel:

========================================================================
struct snd_ac97_build_ops {
	int (*build_3d) (struct snd_ac97 *ac97);
	int (*build_specific) (struct snd_ac97 *ac97);
	int (*build_spdif) (struct snd_ac97 *ac97);
	int (*build_post_spdif) (struct snd_ac97 *ac97);
#ifdef CONFIG_PM
	void (*suspend) (struct snd_ac97 *ac97);
	void (*resume) (struct snd_ac97 *ac97);
#endif
	void (*update_jacks) (struct snd_ac97 *ac97);	/* for jack-sharing */
};
========================================================================

Such #ifdef'ed structures abound in the Linux kernel. We had a similar
situation with the *whole* structure consisting of #ifdef'ed segments. I
had naively assumed that word alignment was preserved regardless of the
preprocessing conditions.

> So I stand by my original statement. Yes, there are incompatibilities,
> but it's easy enough to write code that's compatible with both. Hmm,
> something tells me I've heard this sort of thing before...

Python++ FTW?


Marko



More information about the Python-list mailing list