Page 1 of 3
[Implemented] AllocateMemory without clearing
Posted: Sun Oct 19, 2008 9:10 am
by HotteHC
Hi Fred.
Would it be posible to make an optional Parameter to the
AllocateMemory-Procedure to dont clear the Allocated Mem.
I made some Tests and this would make an Speed-Increment at Factor 3
and more.
Posted: Sun Oct 19, 2008 2:30 pm
by Psychophanta
+1
Looks like ReAllocateMemory() does not clear mem contents, just leave it intact, which AllocateMemory() should do by default :roll:
Posted: Sun Oct 19, 2008 4:47 pm
by netmaestro
ReAllocateMemory leaves the contents intact for good reason, in fact if it didn't it would be more or less useless. It allows to resize the memory block up or down to fit the needed contents, which are usually at least partially existing. If AllocateMemory didn't clear the contents first, you'd be working in a memory block full of random junk which could easily masquerade as the stuff you're looking for in there, introducing some colossally hair-pulling bugs.
Posted: Sun Oct 19, 2008 4:52 pm
by Psychophanta
netmaestro wrote:If AllocateMemory didn't clear the contents first, you'd be working in a memory block full of random junk which could easily masquerade as the stuff you're looking for in there, introducing some colossally hair-pulling bugs.
The stuff to look for there is nothing, at least we firstly fill it with something.
Posted: Sun Oct 19, 2008 4:54 pm
by netmaestro
I mean after you fill it, the block will contain your stuff + random junk.
Posted: Sun Oct 19, 2008 4:55 pm
by Psychophanta
But is at the hand of the programmer to know or not to know what he is doing. So an optional parameter like #PB_ZeroMemory or so, would be great :roll: .
Posted: Sun Oct 19, 2008 5:52 pm
by Kaeru Gaman
I agree with Psychophanta, that a Programmer should know halfways what's in Mem he allocated.
but I would strongly recommend to leave the AllocateMemory default to clearing, to help beginners avoid problems.
leaving allocated memory unchanged (filled with junk) to be more performant should be an explicitly requested feature.
the optional Flag should be set to leave the mem unchanged, not to clear it.
Posted: Sun Oct 19, 2008 10:18 pm
by HotteHC
The fact, why I ask is, in some Situations the Memoryallocation is slow.
F.e. : I had to Allocate an Memoryblock with a size of 256000 very often and it seems to do so very slow. If i use HeapAlloc_ with the Parameter
#HEAP_ALIGN_16 it's much faster then HeapAlloc_ with Parameter
#HEAP_ALIGN_16|#HEAP_ZERO_MEMORY.
Posted: Sun Oct 19, 2008 10:23 pm
by PB
> If AllocateMemory didn't clear the contents first, you'd be working in a
> memory block full of random junk which could easily masquerade as the
> stuff you're looking for in there
Rubbish. If you allocate but haven't then poked anything into it, then you
know it's just random stuff and not your own. :roll:
Posted: Sun Oct 19, 2008 10:37 pm
by ts-soft
How often allocates you memory in a program. I see no real speed advantage

Posted: Sun Oct 19, 2008 10:38 pm
by netmaestro
netmaestro wrote:I mean after you fill it, the block will contain your stuff + random junk.
Posted: Sun Oct 19, 2008 10:54 pm
by PB
Still doesn't matter, because if you fill it fully then it won't have junk, and if
you fill it partially then you know where you've filled and thus where the junk
still is. You know that, nm.

Posted: Sun Oct 19, 2008 11:03 pm
by Kaeru Gaman
yeah, there is no reason to argue against the possibility to allocate without clearing...
only thing to talk about is, wich option should be default.
Posted: Mon Oct 20, 2008 6:12 am
by Little John
As Netmaestro wrote, the desired goal can already be achieved with ReAllocateMemory(), no?
Regards, Little John
Posted: Mon Oct 20, 2008 1:12 pm
by Trond
ReAllocateMemory() also zeros the memory that is newly allocated.
Code: Select all
*A = AllocateMemory(24)
PokeS(*A, ReplaceString(Space(23), "a")) ; Fill *A with "a" characters
*B = ReAllocateMemory(*A, 1024)
The first 23 bytes of *B will now contain "a" characters, the 24th byte will contain the 0 from PokeS(), and the last 1000 bytes will contains zeros from ReAllocateMemory().