customized "new" and pointer alignment

%u new at new.com
Mon Jan 29 15:28:01 PST 2007


no answer from digitalmars.D.learn, try it here.

== Posted at 2007/01/29 15:52 to digitalmars.D.learn

I want to do explicit memory allocation for some of my objects,

I'm reading:  http://digitalmars.com/d/memory.html#newdelete

which says:

# The pointer returned from new() must be to memory aligned to the default
alignment. This is 8 on win32 systems.

Then on the next section:

http://digitalmars.com/d/memory.html#markrelease

new(size_t sz)
    {   void *p;

 p = &buffer[bufindex];
 bufindex += sz;
 if (bufindex > buffer.length)
     throw new OutOfMemory;
 return p;
    }

Is this code correct? I mean the object size (sz) could be any integer, how
can one ensure the alignment requirement?

If the above code in "Mark/Release" is incorrect, can anyone tell me how to
return aligned memory pointers?  and for lots of small objects, does alignment
waste too much memory?

Thanks.




More information about the Digitalmars-d mailing list