Keith Thompson (kst-) wrote:
:
(Malcolm Dew-Jones) writes:
: > Flash Gordon () wrote:
: > : Paminu wrote:
: > : > Is there no such a thing as an ArrayOutOfBounds exception in C?
: >
: > : No. Some implementations will trap some out of bounds accesses under
: > : some circumstances, but it is not guaranteed and I'm not aware of any
: > : that will catch all out of bounds accesses.
: >
: > You wouldn't want a compiler to catch all out of bounds errors. A common
: > idiom for structures is
: >
: > typedef struct _something
: > {
: > struct _something * next;
: > char data[1];
: > } something ;
: Leading underscores in identifiers. Ick. (I'm too lazy to check
: whether this particular usage is a problem.)
"problem"? perhaps that's something new. You (used to) need a name for
the struct but the typedef name is the name you will normally use. I
think I copied the style (underscore on the struct name) from Atari or
Macintosh code.
: > When used, an instance will be malloc'd with a size large enough to
: > contain your data and then the data member can be accessed as
: >
: > a_something.data[i]
: >
: > The array index may commonly appear to be "out of bounds" according to the
: > definition, but doesn't represent a problem.
: It's a common idiom, known as the "struct hack", but strictly speaking
: it invokes undefined behavior. A checking implementation could
: legally forbid references to any element of a_something.data other
: than a_something.data[0].
: C99 added a "safe" version of the "struct hack", known as a "flexible
: array member". For example:
: struct something {
: struct something *next;
: char data[];
: };
Shows how much I use C these days. Thats good to know. I guess I should
bone up on the latest standard (perhaps after learning python and JSP 2).
--
This programmer available for rent.