Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #55268

Re: Pointer to structure

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.lang.c
Subject Re: Pointer to structure
Date 2014-12-22 06:23 +0000
Organization Aioe.org NNTP Server
Message-ID <m78dd2$dvn$1@speranza.aioe.org> (permalink)
References (3 earlier) <Iuzlw.227520$p95.10761@fx38.am4> <chine.bleu-CBB10B.05052221122014@news.eternal-september.org> <lHzlw.145625$c73.56007@fx27.am4> <m77prn$d50$1@speranza.aioe.org> <nkKlw.823906$DX.466140@fx06.iad>

Show all headers | View raw


Richard Damon <Richard@damon-family.org> wrote:

(snip, I wrote)
>> There is a common C trick, likely UB in the standard, but usually
>> works, to declare an array, usually [1] at the end of the struct,
>> and then allocate it an apprropriate amount of memory.

>> struct ps {
>>     char length;
>>     char data[1];
>>     }

>> int n=80;
>> struct ps *x=malloc(sizeof(*x)-1+n);

(snip)

> Actually, C99 codified something almost like this.

Last time I used it was before 1999. 

Some years ago, I was using the Metagraphics package, which had
many structs that worked this way.
 
> struct ps {
>   unsigned char length;
>   char data[]; // Note, empty brackets
> };
 
> struct ps *x = malloc(sizeof(*x) + n);
> *x->length = n;

And it nicely avoids the -1 that is needed the other way.

-- glen

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Pointer to structure Dexterr <dejangru@gmail.com> - 2014-12-21 03:16 -0800
  Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 12:09 +0000
    Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 04:39 -0800
      Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 12:59 +0000
        Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 05:05 -0800
          Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 13:12 +0000
            Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 05:55 -0800
              Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 14:31 +0000
                Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 06:47 -0800
                Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 20:10 +0000
                Re: Pointer to structure Geoff <geoff@invalid.invalid> - 2014-12-21 10:00 -0800
                Re: Pointer to structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 22:44 +0000
                Re: Pointer to structure raltbos@xs4all.nl (Richard Bos) - 2014-12-21 18:01 +0000
                Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 21:21 +0000
                Re: Pointer to structure raltbos@xs4all.nl (Richard Bos) - 2014-12-22 12:11 +0000
                Re: Pointer to structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 22:36 +0000
              Re: Pointer to structure Stephen Sprunk <stephen@sprunk.org> - 2014-12-27 17:56 -0600
                Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-27 16:33 -0800
                Re: Pointer to structure Stephen Sprunk <stephen@sprunk.org> - 2015-01-15 12:29 -0600
            Re: Pointer to structure glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-22 00:50 +0000
              Re: Pointer to structure Richard Damon <Richard@Damon-Family.org> - 2014-12-21 20:19 -0500
                Re: Pointer to structure glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-22 06:23 +0000
                Re: Pointer to structure gazelle@shell.xmission.com (Kenny McCormack) - 2014-12-22 12:03 +0000
          Re: Pointer to structure gazelle@shell.xmission.com (Kenny McCormack) - 2014-12-21 13:20 +0000
            Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 05:40 -0800
  Re: Pointer to structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 13:16 +0000
  Re: Pointer to structure Barry Schwarz <schwarzb@dqel.com> - 2014-12-21 10:02 -0800
  Re: Pointer to structure Keith Thompson <kst-u@mib.org> - 2014-12-21 12:25 -0800
    Re: Pointer to structure Dexterr <dejangru@gmail.com> - 2014-12-21 15:07 -0800

csiph-web