Page 1 of 2

FreeMemory() should zero the pointer

Posted: Sun Nov 12, 2006 6:01 pm
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.

Posted: Sun Nov 12, 2006 6:15 pm
by Henrik
Hi netmaestro
yes seems logic

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

best Henrik

Posted: Sun Nov 12, 2006 6:16 pm
by netmaestro
Crap! I meant to put it there, I missed. Could a mod please move it?

Re: FreeMemory() should zero the pointer

Posted: Sun Nov 12, 2006 6:18 pm
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".

Posted: Sun Nov 12, 2006 6:24 pm
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

Posted: Sun Nov 12, 2006 6:30 pm
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?

Posted: Sun Nov 12, 2006 6:30 pm
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.

Posted: Sun Nov 12, 2006 6:33 pm
by Henrik
I know dell_jockey :wink:

but use instead

Code: Select all

if MemorySize(*buffer) 
Henrik

Posted: Sun Nov 12, 2006 6:34 pm
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.

Posted: Sun Nov 12, 2006 6:38 pm
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

Re: FreeMemory() should zero the pointer

Posted: Sun Nov 12, 2006 6:40 pm
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).

Posted: Sun Nov 12, 2006 6:41 pm
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.

Posted: Sun Nov 12, 2006 6:41 pm
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.

Posted: Sun Nov 12, 2006 6:48 pm
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

Re: FreeMemory() should zero the pointer

Posted: Sun Nov 12, 2006 7:01 pm
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