home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / c / 2636 < prev    next >
Encoding:
Text File  |  1992-09-15  |  1.2 KB  |  30 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!think.com!spdcc!dirtydog.ima.isc.com!karl
  3. From: karl@ima.isc.com (Karl Heuer)
  4. Subject: Re: Memory alignment
  5. Message-ID: <1992Sep15.225728.9993@ima.isc.com>
  6. Sender: usenet@ima.isc.com (news)
  7. Organization: Interactive Systems, Cambridge, MA 02138-5302
  8. References: <lbc3neINNtt@news.bbn.com>
  9. Date: Tue, 15 Sep 1992 22:57:28 GMT
  10. Lines: 18
  11.  
  12. In article <lbc3neINNtt@news.bbn.com> langevin@indian.bbn.com. (Tim Langevin) writes:
  13. >   Is the following piece of code conforming ANSI C?
  14.  
  15. I believe so, but only because you didn't do anything with the pointer you
  16. generated.  If you want it to be a general-purpose memory allocator, then it's
  17. conceivable that it fails when you try to use it for some type not included in
  18. your ALL_TYPES union.
  19.  
  20. >   Is there a better way to do this?
  21.  
  22. I would omit the alignment object, and change the size of the allocation
  23. request to be (sizeof(HEADER)+ALIGN-1)/ALIGN*ALIGN + user_size, where ALIGN is
  24. a constant that you configure appropriately on each system.  (This is how
  25. malloc() itself is often written.)
  26.  
  27. It's fairly clean, and it's portable, even if it isn't strictly conforming.
  28.  
  29. Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint
  30.