FreeMemory() should zero the pointer

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

FreeMemory() should zero the pointer

Post by netmaestro »

After *buffer = AllocateMemory(1024), *buffer contains a pointer to the address of the memory block. After FreeMemory(*buffer), the value of the pointer is of no conceivable use anymore. Imho it would be cleaner and more intuitive if after FreeMemory(*buffer), the programmer could test *buffer and find it's 0 now.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

Hi netmaestro
yes seems logic

But shouldn't this go' her "Feature Requests and Wishlists" :wink:

best Henrik
Last edited by Henrik on Sun Nov 12, 2006 6:16 pm, edited 1 time in total.
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 »

Crap! I meant to put it there, I missed. Could a mod please move it?
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Re: FreeMemory() should zero the pointer

Post by dell_jockey »

netmaestro wrote:After *buffer = AllocateMemory(1024), *buffer contains a pointer to the address of the memory block. After FreeMemory(*buffer), the value of the pointer is of no conceivable use anymore. Imho it would be cleaner and more intuitive if after FreeMemory(*buffer), the programmer could test *buffer and find it's 0 now.
Since *buffer belongs to you, there's nothing that keeps you from setting *buffer to zero yourself, directly after you did a FreeMemory(*buffer). In fact, that's what I implemented with a debugging macro.
I use this zeroed pointer for debugging checks only, since in normal code, there is no need to dereference this pointer. As you pointed out "the value of the pointer is of no conceivalble use anymore".
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

@dell_jockey
No i agree with netmaestro, it's just an empty pointer to no were after FreeMemory(*buffer)
And the size of MemorySize(*buffer) is -1 so why not set *buffer to -1

best Henrik
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 may own it, but I didn't put the value in it - AllocateMemory() did. Doesn't it make sense that FreeMemory() should clear it?
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Hi Henrik,

it's not that I disagree with the two of you; I was merely pointing out a work-around that you could use until your request gets implemented.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

I know dell_jockey :wink:

but use instead

Code: Select all

if MemorySize(*buffer) 
Henrik
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 »

the size of MemorySize(*buffer) is -1 so why not set *buffer to -1
I believe 0 is a better value, as *buffer held 0 before the AllocateMemory. Also, "If *buffer.." will test true if the value is -1, and for those who want to test it instead of using MemorySize, it should really test false imho.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

@netmaestro
Okay but is'nt 0 a relevant address in Memory ??
well i don't just thought it was..

But i agree it should be reset.

best Henrik
Last edited by Henrik on Sun Nov 12, 2006 6:42 pm, edited 1 time in total.
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Re: FreeMemory() should zero the pointer

Post by Konne »

Imho it would be cleaner and more intuitive if after FreeMemory(*buffer), the programmer could test *buffer and find it's 0 now.
That is simply impossible.
The function gets a copy of the 4 Byte Long Value "*Buffer" and not the original. So it can't change it (Only if it would be a macro but u can write that yourself).
Apart from that Mrs Lincoln, how was the show?
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Henrik wrote:I know dell_jockey :wink:

but use instead

Code: Select all

if MemorySize(*buffer) 
Henrik
I considered using the same thing, but then realised that this code makes assumptions about the value of True & False. These values might change in future compiler versions, that's why I specifically set the pointer to zero myself. It's a bit more verbose, but survives unexpected compiler changes.
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

Henrik wrote:@netmaestro
Okay but is'nt 0 a relevant address in Memory ??
well i don't just thought it was..

best Henrik
Not it isn't (the only Value).
It's used to Simpolize a Pointer isn't valid.
Apart from that Mrs Lincoln, how was the show?
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

netmaestro wrote:I may own it, but I didn't put the value in it - AllocateMemory() did. Doesn't it make sense that FreeMemory() should clear it?
of course you put the value in the pointer, you use the "=" operator to do this
there's no way freememory should empty the pointer for you

Dri
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Re: FreeMemory() should zero the pointer

Post by Henrik »

@Konne
Konne wrote: Not it isn't (the only Value).
It's used to Simpolize a Pointer isn't valid.
Okay..
That is simply impossible.
The function gets a copy of the 4 Byte Long Value "*Buffer" and not the original. So it can't change it (Only if it would be a macro but u can write that yourself).
Well okay then, i don't know what go's on behind the scene.
i find it odd that it can't be zero out after FreeMemory, but if you say so, i really don't know, but thanks for the explanation anyway :D



** Edit **
Well i ment if you can do

Code: Select all

 FreeMemory(*FileBuffer1a)
*FileBuffer1a=0
Why can't the compiler ?
but if it can't be done, because of some technically "things" or something :D , then no big deal anyway, just would be nice:wink:

Best Regads Henrik
Last edited by Henrik on Sun Nov 12, 2006 7:11 pm, edited 1 time in total.
Post Reply