home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / c / 3421 < prev    next >
Encoding:
Text File  |  1993-01-25  |  2.7 KB  |  52 lines

  1. Newsgroups: comp.std.c
  2. Path: 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
  3. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  4. Subject: Re: Is this allowed in C?
  5. Message-ID: <1993Jan26.010949.19453@nntpd.lkg.dec.com>
  6. Sender: usenet@nntpd.lkg.dec.com (USENET News System)
  7. Reply-To: diamond@jit.dec.com (Norman Diamond)
  8. Organization: Digital Equipment Corporation Japan , Tokyo
  9. References: <C17p4n.Dx8@undergrad.math.waterloo.edu> <1993Jan25.020122.22998@nntpd.lkg.dec.com> <553@heimdall.sdrc.com>
  10. Date: Tue, 26 Jan 1993 01:09:49 GMT
  11. Lines: 39
  12.  
  13. In article <553@heimdall.sdrc.com> scjones@thor.sdrc.com (Larry Jones) writes:
  14. >In article <1993Jan25.020122.22998@nntpd.lkg.dec.com>, diamond@jit345.bad.jit.dec.com (Norman Diamond) writes:
  15. >> The C standard guarantees that whenever malloc() succeeds, the value will
  16. >> point to storage suitably aligned for everything, ....
  17.  
  18. >...that is no larger than the amount of space allocated.  Strictly
  19. >speaking, if you only allocate 1 byte, the returned pointer need not
  20. >be suitably aligned for types that are larger than 1 byte (e.g. int
  21. >or double), since you can't store them there.
  22.  
  23. I half-agree.  ANSI Classic section 4.10.3, page 155 lines 13 to 15:
  24. "The pointer returned if the allocation succeeds is suitably aligned so that
  25. it may be assigned to a pointer to any type of object and then used to access
  26. such an object or an array of objects in the space allocated [...]"
  27.  
  28. So if int, double, etc. have size larger than 1, and if int, double, etc.
  29. require stricter alignment than char, then there are still two possibilities.
  30. If pointers to them must have some low-order zero bits (which some hardware
  31. architectures require) then malloc() must still suitably align the allocated
  32. byte even though it's a single byte.  However, if pointers can contain any
  33. random bits (not failing until an attempt is made to dereference them) then
  34. the byte need not be aligned.  In either case, standard C does not permit
  35. dereferencing a pointer to an object that doesn't actually exist there.
  36.  
  37. ---------------------
  38.  
  39. Correction to my previous post:  Someone pointed out that the second post by
  40. that student (whose name I have lost) contained the following expression:
  41. >>> store = malloc(sizeof(blah)+3) + 3;
  42.  
  43. I didn't notice that one of the additions was outside of the parentheses.
  44. In fact, the second + is illegal because a pointer-to-void cannot be an
  45. operand of +, so the rest of the question was moot.
  46. (However, malloc(sizeof(blah)+3) must still point to suitably aligned space
  47. or else be null.)
  48. --
  49. Norman Diamond                diamond@jit.dec.com
  50. If this were the company's opinion, I wouldn't be allowed to post it.
  51. Pardon me?  Or do I have to commit a crime first?
  52.