[Implemented] AllocateMemory without clearing

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

[Implemented] AllocateMemory without clearing

Post 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.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

+1

Looks like ReAllocateMemory() does not clear mem contents, just leave it intact, which AllocateMemory() should do by default :roll:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I mean after you fill it, the block will contain your stuff + random junk.
BERESHEIT
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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: .
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

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.
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

netmaestro wrote:I mean after you fill it, the block will contain your stuff + random junk.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

As Netmaestro wrote, the desired goal can already be achieved with ReAllocateMemory(), no?

Regards, Little John
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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().
Post Reply