From 3954682790080721213
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,855073145fb9fdb7
X-Google-Attributes: gidf78e5,public
From: jkanze@otelo.ibmmail.com
Subject: Re: Minimum Size of Array in Standard (solution)
Date: 1998/11/05
Message-ID: <71sq58$sg8$1@nnrp1.dejanews.com>#1/1
X-Deja-AN: 408832799
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <199811032027.NAA25799@ncar.ucar.EDU> <71p6m5$4m1$1@shell7.ba.best.com>
X-Original-Date: Thu, 05 Nov 1998 18:18:16 GMT
X-Http-User-Agent: Mozilla/4.05 [de] (WinNT; I)
X-Complaints-To: news@news.unimelb.edu.au
X-Http-Proxy: 1.0 x10.dejanews.com:80 (Squid/1.1.22) for client 193.194.7.85
X-Trace: izvestia.its.unimelb.edu.au 910308582 32305 128.250.29.16 (5 Nov 1998 23:29:42 GMT)
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Thu Nov 05 18:18:16 1998 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANkI0zeEDnX0m9pzZAQFi8gF+OWKTXBZXgf+wfZk3vaO/c57MWQlVcIvQ rDfU39bvCNDpcyVNdMtUbcjzVSV5zqKp =iRdJ
NNTP-Posting-Date: 5 Nov 1998 23:29:42 GMT
Newsgroups: comp.std.c++

In article <71p6m5$4m1$1@shell7.ba.best.com>,
  ncm@nospam.cantrip.org (Nathan Myers) wrote:
> >> "Jim Cobban" <Jim.Cobban.jcobban@nt.com> writes:
> >> ...
> >> |>  This is used in a lot of existing code to
> >> |>  represent an array whose size is known by some implementation specific
> >> |>  method, for example in a structure such as the following:
> >> |>
> >> |>   struct varstruct
> >> |>   {
> >> |>     unsigned int	num_elements;
> >> |>     int		elements[0];
> >> |>   };
> >>
> >> This code is illegal (in C).
>
> There is a standard-conforming way to get a similar effect.
>
>   template <class T>
>     struct varstruct
>   {
>     union {
>       unsigned int num_elements;
>       T dummy[(sizeof(unsigned int)+sizeof(T)-1)/sizeof(T)];
>     };
>     T* elements() { return (T*)(char*)(this+1); }
>   };
>
> The dummy array ensures that (this+1) is aligned properly for a T.
> If you have more data members than just num_elements, you can put
> them in a struct and use that in the sizeof expression in place of
> unsigned int.
>
> Of course you have to make sure there is usable storage at this[1].

I believe I posted something along these lines many years back; I actually
use this in the implementation of my string class.

Of course, while fine for string, you will need to use something else in
the union for the more general case, where T might have a constructor.
(I declare a union of all of the built in types, plus a few pointers, under
the name GB_MaxAlign, in a global header file.  It's not guaranteed by the
standard, but I really cannot imagine an implementation where some derived
type had stricter alignment requirements than any of the builtin types.)

--
James Kanze                         email: kanze@gabi-soft.fr

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



