Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wasatch!cs.utexas.edu!usc!bloom-beacon!adam.pika.mit.edu!scs From: scs@adam.pika.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: malloc(5) (wasn't: Re: "do ... while ((NULL + 1) - 1);" ...) Message-ID: <13657@bloom-beacon.MIT.EDU> Date: 20 Aug 89 06:04:22 GMT References: <1527@cbnewsl.ATT.COM> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: scs@adam.pika.mit.edu (Steve Summit) Lines: 28 In article <1527@cbnewsl.ATT.COM> dfp@cbnewsl.ATT.COM (david.f.prosser) writes: >Strictly speaking, malloc must return a pointer to an object that >can be accessed by a type commensurate with its size in bytes. >Moreover, it may well be possible to argue that unless the requested size >is a multiple of the size of an int, the returned pointer need not be >aligned appropriately for an int. For example, ``malloc(5)''. I hope not. This would break the variable-sized structure trick (discussed here at length not long ago): struct string { int length; char text[1]; /* actually text[length] */ }; where we allocate a "string" with something like: (struct string *)malloc(sizeof(struct string)-1+stringlen) Henry Spencer says that Dennis Ritchie calls this "unwarranted chumminess with the compiler," but it's widely used. (Note that (struct string *)malloc(sizeof(struct string)) _would_ work, since sizeof(struct string) will typically be 2*sizeof(int).) Steve Summit scs@adam.pika.mit.edu