From: Hans-Bernhard Broeker Newsgroups: comp.os.msdos.djgpp Subject: Re: one more about memory alloc - free memory of needed amount only Date: 24 Aug 2000 08:23:17 GMT Organization: Aachen University of Technology (RWTH) Lines: 34 Message-ID: <8o2m1l$6tb$1@nets3.rz.RWTH-Aachen.DE> References: <39A4EB1B DOT 29871 DOT 12C6F4 AT localhost> NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de X-Trace: nets3.rz.RWTH-Aachen.DE 967105397 7083 137.226.32.75 (24 Aug 2000 08:23:17 GMT) X-Complaints-To: abuse AT rwth-aachen DOT de NNTP-Posting-Date: 24 Aug 2000 08:23:17 GMT Originator: broeker@ To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Leon AT caresystems DOT com DOT au wrote: > Hello sorry to revisit this subejct but > a day or two ago i asked about possibility of alligning memory so > that say malloc(25) returns address x and then one modulates this > address so that it is alligned to a multiple of say 64 bytes. (by > making malloc(25+63)) You don't modulate it after the fact. You make the original request larger so that a suitable block of the required length and alignment is guaranteed to be found inside the block returned by malloc(). I.e. if you want a block of 300 bytes aligned to a 64-byte boundary, you would: unsigned char * realptr = malloc(300+63); some_type * ptr_to_use = (some_type *) (realptr + (((unsigned long) realptr) % 64)); > the problem is - how would one free memory that is a remainder of offset? One wouldn't. You can't do that. The memory sacrificed for alignment is lost. Which is one of the reasons why this is so rarely used. The gains from better alignment often won't compensate the additional hassles needed to set it up (two pointers to store, loss of potentially large fractions of memory). Linux systems have a special function 'memalign' to allocate such blocks with a particular alignment, which does essentially the same as described above, but without you having to do it by hand. -- Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de) Even if all the snow were burnt, ashes would remain.