Re: Empty structures have sizeof(1) in C++ now ?

From: Michael Gmelin <grembo_at_freebsd.org>
Date: Thu, 20 Feb 2025 11:03:39 UTC

On Thu, 20 Feb 2025 09:40:54 +0000
Poul-Henning Kamp <phk@phk.freebsd.dk> wrote:

> Is this a bug ?
> 
> 
> 	critter phk> cat /tmp/_.c
> 
> 	#include <stdio.h>
> 	struct foo {
> 	};
> 
> 	int
> 	main(int argc, char **argv)
> 	{
> 		struct foo bar;
> 		printf("%jd %jd\n", sizeof(struct foo), sizeof(bar));
> 		return (0);
> 	}
> 	critter phk> cc -o /tmp/a.out /tmp/_.c
> 	critter phk> /tmp/a.out
> 	0 0
> 	critter phk> c++ -o /tmp/a.out /tmp/_.c
> 	c++: warning: treating 'c' input as 'c++' when in C++ mode,
> this behavior is deprecated [-Wdeprecated] critter phk> /tmp/a.out
> 	1 1
> 

AFAIK it has always been like that, see
https://www.stroustrup.com/bs_faq2.html#sizeof-empty :

     To ensure that the addresses of two different objects will be
     different. For the same reason, "new" always returns pointers to
     distinct objects.

-m

-- 
Michael Gmelin