home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: Is this ANSI?
- Message-ID: <1992Nov23.090611.3209@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <24238@alice.att.com> <1992Nov21.125038.26244@sq.sq.com> <24270@alice.att.com>
- Date: Mon, 23 Nov 92 09:06:11 GMT
- Lines: 54
-
- Andrew and I have been crossing postings; the one I'm responding to
- now contains a wrong prediction of what I would say in my previous
- posting. I'll just address that point.
-
- > Unless both the pointer operand and the result point to
- > elements of the same array object, or the pointer
- > operand points one past the last element of an array
- > object and the result points to an element of the same
- > array object, the behavior is undefined if the result
- > is used as an operand of the unary * operator.
- >
- > Now, look back at the assertion I asked you to remember. I expect
- > that Mark Brader is going to say `But p->data isn't an array of
- > one element, it's an array of 101 elements. After all, when you
- > said malloc(sizeof(struct FM + 100), you allocated enough memory
- > for 101 elements!'
-
- No, I'm not. That would be wrong for the reasons you said. What I
- did say was that while the character pointed to by the decayed
- pointer p->data is indeed part of the one-character array p->data,
- it is *also* part of a larger array, namely the object returned by
- malloc(), whose size might be, say, 104 elements when considered
- as an array of char. Therefore the Unless clause is applicable
- and the reference valid.
-
- > struct FM *q = (struct FM *) malloc(sizeof(struct FM) + 100);
- > ...
- > *p = *q;
-
- Agreed, this should not copy the 100 extra bytes.
-
- > Another way to look at it is this. Suppose I have a compiler that rejects:
- >
- > void f(struct FM *p)
- > {
- > p->data[5] = '?';
- > }
-
- Then, as they say,"your compiler is broken, and you should ask for a
- refund". This is Yet Another Way in which C's very general pointer
- types inhibit the compiler from doing things that you might like it to.
-
- Of course, your system is free to abort at *run* time in f(), if it
- finds that the argument passed to f() was merely a pointer to a
- struct FM, and not to a larger malloc()ed area. Saber-C, or whatever
- they call it these days, would get this right.
- --
- Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com
- A standard is established on sure bases, not capriciously but with
- the surety of something intentional and of a logic controlled by
- analysis and experiment. ... A standard is necessary for order
- in human effort. -- Le Corbusier
-
- This article is in the public domain.
-