Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #110324
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Help on this typedef |
| Date | 2017-05-18 20:02 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87fug2yond.fsf@bsb.me.uk> (permalink) |
| References | (4 earlier) <faaaf47a-51ec-4323-aba2-18cabef07fe8@googlegroups.com> <kfnd1bcsb3d.fsf@x-alumni2.alumni.caltech.edu> <7828b0dd-de46-40a2-9636-81117d1f17f6@googlegroups.com> <kfninl2nqqy.fsf@x-alumni2.alumni.caltech.edu> <799899ab-ccf3-49a4-a928-d405dc142fff@googlegroups.com> |
joel.rees@gmail.com writes:
> On Monday, May 15, 2017 at 11:23:23 PM UTC+9, Tim Rentsch wrote:
>> joel.rees@somewhere writes:
>>
>> > On Sunday, May 14, 2017 at 6:25:48 AM UTC+9, Tim Rentsch wrote:
>> >> joel.rees@somewhere writes:
>> [...]
>> >>> An indefinite array type has more limitations than a type of an
>> >>> array with one element.
>> >>
>> >> In some ways it has more, in other ways it has fewer.
>> >>
>> >>> Both, of course, can refer to an array of more than one element
>> >>> when instantiated.
>> >>
>> >> What do you mean? AFAIK there is no way to "instantiate" an
>> >> indefinite array type.
>> >
>> > Bad phrasing, I guess. "... when in use." probably would have been
>> > more accurate.
>>
>> Ah. In that case I must demur regarding part of your later
>> statement. An access through an array type of extent 1 is
>> not definedly allowed to access elements after the element
>> at index 0.
>
> typedef string_header_s struct
> { short length; /* limited length */
> char text[ 1 ];
> } string_header_t;
>
> And before you C++ types came around, we just left the array size
> out: char text[].
No, the old idiom as an array length 1 as above. The form sanctioned by
the newer C standards (C99 and up) is to leave the size out, provided
this is the last member of a struct. (I don't know what C++ has to do
with it -- I took "you C++ types" to refer to people, but I don't know
who.)
> But that was declared in all cases to be equivalent
> to char *, so it means something else, now. And if that's what you
> mean in such a header, you say char *, instead.
No. char x[] and char *x mean the same thing in a parameter declaration
but not elsewhere, and definitely not in a struct!
<snip>
> typedef struct string_header_s
> { short length;
> char string[ 1 ];
> } string_header_t;
>
> char bigblock[ 100000 ];
> char * here = bigblock;
>
> string_header_t * string_allocate( long length )
> { char * place = here;;
> if ( ( place = here + length + sizeof (string_header_t) ) >= bigblock + 100000 )
This test is, technically, wrong. Most people would not worry about it,
but once you know what C guarantees and what it does not guarantee it's
hard to go back[1]. I'd just keep a counter of the bytes used to avoid
a potentially undefined pointer construction and test.
> { return NULL;
> }
> here = place;
> ( (string_header_t *) place )->length = length;
> return (string_header_t *) place;
> }
You also have potential alignment problems.
<snip>
[1] You can only generate pointers that point into, or just past, an
array, and pointer can only be compared with <, <=, > and >= if they
point into the same object (or just past).
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Help on this typedef fl <rxjwg98@gmail.com> - 2017-05-10 19:12 -0700
Re: Help on this typedef Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-05-10 21:06 -0600
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-10 20:11 -0700
Re: Help on this typedef David Brown <david.brown@hesbynett.no> - 2017-05-11 10:03 +0200
Re: Help on this typedef supercat@casperkitty.com - 2017-05-11 08:35 -0700
Re: Help on this typedef David Brown <david.brown@hesbynett.no> - 2017-05-12 10:34 +0200
Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 06:01 -0700
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-12 09:38 -0700
Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 05:08 -0700
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-13 14:25 -0700
Re: Help on this typedef joel.rees@gmail.com - 2017-05-14 23:36 -0700
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-15 07:23 -0700
Re: Help on this typedef joel.rees@gmail.com - 2017-05-18 07:40 -0700
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-18 09:35 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-18 10:59 -0700
Re: Help on this typedef joel.rees@gmail.com - 2017-05-18 23:40 -0700
Re: Help on this typedef jadill33@gmail.com - 2017-05-19 08:12 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 08:41 -0700
Re: Help on this typedef jadill33@gmail.com - 2017-05-19 09:41 -0700
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 09:34 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 10:18 -0700
Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-19 21:35 +0100
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 14:16 -0700
Re: Help on this typedef Ike Naar <ike@iceland.freeshell.org> - 2017-05-19 21:15 +0000
Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-19 23:54 +0100
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 14:25 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 15:18 -0700
Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-19 23:52 +0100
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 16:42 -0700
Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-20 01:16 +0100
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-20 01:58 -0700
Re: Help on this typedef joel.rees@gmail.com - 2017-05-20 02:25 -0700
Re: Help on this typedef joel.rees@gmail.com - 2017-05-19 22:09 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 23:23 -0700
Re: Help on this typedef "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-19 13:33 -0400
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 13:44 -0700
Re: Help on this typedef jameskuyper@verizon.net - 2017-05-19 19:20 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 23:30 -0700
Re: Help on this typedef jameskuyper@verizon.net - 2017-05-20 07:20 -0700
Re: Help on this typedef jameskuyper@verizon.net - 2017-05-20 07:46 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-22 10:50 -0700
Re: Help on this typedef "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-18 12:37 -0400
Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-18 20:02 +0100
Re: Help on this typedef joel.rees@gmail.com - 2017-05-19 00:20 -0700
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 09:51 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 10:31 -0700
Re: Help on this typedef Ike Naar <ike@iceland.freeshell.org> - 2017-05-19 19:48 +0000
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-20 08:47 -0700
Re: Help on this typedef "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-05-27 18:50 +0200
Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 06:17 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-12 08:46 -0700
Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 18:11 -0700
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-12 18:37 -0700
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-13 09:44 -0700
Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-13 13:47 -0700
Re: Help on this typedef Robert Wessel <robertwessel2@yahoo.com> - 2017-05-14 00:28 -0500
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 13:39 -0700
Re: Help on this typedef raltbos@xs4all.nl (Richard Bos) - 2017-05-15 13:02 +0000
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-15 07:30 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-12 20:45 -0700
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-13 09:57 -0700
Re: Help on this typedef Philip Lantz <prl@canterey.us> - 2017-05-13 12:07 -0700
Re: Help on this typedef David Brown <david.brown@hesbynett.no> - 2017-05-14 15:34 +0200
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 12:48 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-15 07:39 -0700
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-24 08:23 -0700
Re: Help on this typedef supercat@casperkitty.com - 2017-05-24 09:17 -0700
Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-27 06:15 -0700
csiph-web