Article View: comp.std.c
Article #3303Re: Is this allowed in C?
From: Norman Diamond
Date: Tue, 26 Jan 1993 01:09
Date: Tue, 26 Jan 1993 01:09
39 lines
2847 bytes
2847 bytes
In article <553@heimdall.sdrc.com> scjones@thor.sdrc.com (Larry Jones) writes: >In article <1993Jan25.020122.22998@nntpd.lkg.dec.com>, diamond@jit345.bad.jit.dec.com (Norman Diamond) writes: >> The C standard guarantees that whenever malloc() succeeds, the value will >> point to storage suitably aligned for everything, .... >...that is no larger than the amount of space allocated. Strictly >speaking, if you only allocate 1 byte, the returned pointer need not >be suitably aligned for types that are larger than 1 byte (e.g. int >or double), since you can't store them there. I half-agree. ANSI Classic section 4.10.3, page 155 lines 13 to 15: "The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of objects in the space allocated [...]" So if int, double, etc. have size larger than 1, and if int, double, etc. require stricter alignment than char, then there are still two possibilities. If pointers to them must have some low-order zero bits (which some hardware architectures require) then malloc() must still suitably align the allocated byte even though it's a single byte. However, if pointers can contain any random bits (not failing until an attempt is made to dereference them) then the byte need not be aligned. In either case, standard C does not permit dereferencing a pointer to an object that doesn't actually exist there. --------------------- Correction to my previous post: Someone pointed out that the second post by that student (whose name I have lost) contained the following expression: >>> store = malloc(sizeof(blah)+3) + 3; I didn't notice that one of the additions was outside of the parentheses. In fact, the second + is illegal because a pointer-to-void cannot be an operand of +, so the rest of the question was moot. (However, malloc(sizeof(blah)+3) must still point to suitably aligned space or else be null.) -- Norman Diamond diamond@jit.dec.com If this were the company's opinion, I wouldn't be allowed to post it. Pardon me? Or do I have to commit a crime first?
Technical Details
Message-ID:
<1993Jan26.010949.19453@nntpd.lkg.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!gatech!usenet.ins.cwru.edu!agate!netsys!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<C17p4n.Dx8@undergrad.math.waterloo.edu> <1993Jan25.020122.22998@nntpd.lkg.dec.com> <553@heimdall.sdrc.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!usenet.ins.cwru.edu!agate!netsys!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Subject: Re: Is this allowed in C? Message-ID: <1993Jan26.010949.19453@nntpd.lkg.dec.com> Sender: usenet@nntpd.lkg.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo References: <C17p4n.Dx8@undergrad.math.waterloo.edu> <1993Jan25.020122.22998@nntpd.lkg.dec.com> <553@heimdall.sdrc.com> Date: Tue, 26 Jan 1993 01:09:49 GMT Lines: 39