🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1208

Re: Memory alignment

#1208
From: Karl Heuer
Date: Tue, 15 Sep 1992 22:57
18 lines
1254 bytes
In article <lbc3neINNtt@news.bbn.com> langevin@indian.bbn.com. (Tim Langevin) writes:
>   Is the following piece of code conforming ANSI C?

I believe so, but only because you didn't do anything with the pointer you
generated.  If you want it to be a general-purpose memory allocator, then it's
conceivable that it fails when you try to use it for some type not included in
your ALL_TYPES union.

>   Is there a better way to do this?

I would omit the alignment object, and change the size of the allocation
request to be (sizeof(HEADER)+ALIGN-1)/ALIGN*ALIGN + user_size, where ALIGN is
a constant that you configure appropriately on each system.  (This is how
malloc() itself is often written.)

It's fairly clean, and it's portable, even if it isn't strictly conforming.

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint
Technical Details
Message-ID: <1992Sep15.225728.9993@ima.isc.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!spdcc!dirtydog.ima.isc.com!karl
References: <lbc3neINNtt@news.bbn.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!spdcc!dirtydog.ima.isc.com!karl
From: karl@ima.isc.com (Karl Heuer)
Subject: Re: Memory alignment
Message-ID: <1992Sep15.225728.9993@ima.isc.com>
Sender: usenet@ima.isc.com (news)
Organization: Interactive Systems, Cambridge, MA 02138-5302
References: <lbc3neINNtt@news.bbn.com>
Date: Tue, 15 Sep 1992 22:57:28 GMT
Lines: 18