[Implemented] AllocateMemory without clearing
[Implemented] AllocateMemory without clearing
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.
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.
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
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.
BERESHEIT
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
The stuff to look for there is nothing, at least we firstly fill it with something.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.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
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.
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.
oh... and have a nice day.
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.
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.
> 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:
> 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:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
How often allocates you memory in a program. I see no real speed advantage 

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
-
- Addict
- Posts: 4777
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
ReAllocateMemory() also zeros the memory that is newly allocated.
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().
Code: Select all
*A = AllocateMemory(24)
PokeS(*A, ReplaceString(Space(23), "a")) ; Fill *A with "a" characters
*B = ReAllocateMemory(*A, 1024)