Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Feb 2025 12:03:39 +0100
From:      Michael Gmelin <grembo@freebsd.org>
To:        Poul-Henning Kamp <phk@phk.freebsd.dk>
Cc:        current@freebsd.org
Subject:   Re: Empty structures have sizeof(1) in C++ now ?
Message-ID:  <20250220120339.2f1af442.grembo@freebsd.org>
In-Reply-To: <202502200940.51K9eswS003142@critter.freebsd.dk>

index | next in thread | previous in thread | raw e-mail



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


help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20250220120339.2f1af442.grembo>