Rename FreeArray() to ClearArray()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Rename FreeArray() to ClearArray()

Post by Little John »

For lists there are ClearList() and FreeList().
For maps there are ClearMap() and FreeMap().

FreeArray() does for arrays what ClearList() does for lists, and what ClearMap() does for maps.
This is not consistent, so better rename FreeArray() to ClearArray(). Also ClearArray() tells better what the function actually does.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Rename FreeArray() to ClearArray()

Post by Josh »

If you want this, after ClearArray() the Array hasn't to be initialized new with DIM, the count of elements has to be changed with REDIM.

Furthermore it would be consistent, allowing to create a "ClearArray" with -1.
See my discussion here.
sorry for my bad english
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Rename FreeArray() to ClearArray()

Post by Fred »

FreeArray() should really free the array, not only clear it.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Rename FreeArray() to ClearArray()

Post by Little John »

Fred wrote:FreeArray() should really free the array, not only clear it.
But as far as I can see it does not do so. FreeArray() only "redims" the array so to say, so that it is empty (has 0 elements).
For this reason, also the current documentation of FreeArray() IMHO is wrong or at least strongly misleading.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Rename FreeArray() to ClearArray()

Post by Josh »

To make Array, Lists and Maps really consistent, there should also be changes in declaration:

Define Array MyArray(0)
Define List MyList()
Define Map MyMap()

instead of:

Define Dim MyArray(0)
Define NewList MyList()
Define NewMap MyMap()
sorry for my bad english
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Rename FreeArray() to ClearArray()

Post by freak »

FreeList() releases the memory of the list elements and the internal list metadata.
FreeArray() releases the memory containing the array elements and the internal array metadata.

Where is the inconsistency?
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Rename FreeArray() to ClearArray()

Post by Psychophanta »

Little John wrote:
Fred wrote:FreeArray() should really free the array, not only clear it.
But as far as I can see it does not do so. FreeArray() only "redims" the array so to say, so that it is empty (has 0 elements).
For this reason, also the current documentation of FreeArray() IMHO is wrong or at least strongly misleading.
I agree with Fred, there is needed real FreeArray(), FreeList()... commands. As the current ones only Redims to 0 elements.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Rename FreeArray() to ClearArray()

Post by Josh »

freak wrote:Where is the inconsistency?
There:

Code: Select all

Dim MyArray(0)
NewList MyList()

Debug ArraySize (MyArray())
Debug ListSize (MyList())

FreeArray (MyArray())
FreeList (MyList())

Debug ArraySize (MyArray())
Debug ListSize (MyList())
You can use the freed array also as parameter in procedures. Imho FreeArray is definitely a ClearArray.
sorry for my bad english
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Rename FreeArray() to ClearArray()

Post by freak »

So your problem is the difference between ListSize() and ArraySize() and not between FreeList() and FreeArray(). Just because ArraySize() has a return value to indicate a freed array doesn't mean that FreeArray() does not actually free the array.

> You can use the freed array also as parameter in procedures.

You can do the same with a list.
quidquid Latine dictum sit altum videtur
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Rename FreeArray() to ClearArray()

Post by Little John »

freak wrote:So your problem is the difference between ListSize() and ArraySize() and not between FreeList() and FreeArray(). Just because ArraySize() has a return value to indicate a freed array doesn't mean that FreeArray() does not actually free the array.
You asked for an inconsistency, and Josh showed you an inconsistency.

He probably thought (and so did I), that the results of ArraySize() and ListSize() are correct and consistent. If so, then there is an inconsistency between FreeArray() and FreeList(). Otherwise there is an inconsistency between ArraySize() and ListSize() (or a bug somewhere).
freak wrote:> You can use the freed array also as parameter in procedures.

You can do the same with a list.
After having tested it now, I see that you are right: After calling FreeList(), the list can still be used without calling NewList. I was misguided by the documentation of FreeList() which is wrong then:
To use this list again later, NewList has to be called for it.
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: Rename FreeArray() to ClearArray()

Post by Thade »

You Guys have Problems ...

What's next?
Clearing or Freeing each Variable?
x.d=1000

Oops this uses 8 Bytes of memory

FreeVariable(x.d)

:?:
:lol:
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Rename FreeArray() to ClearArray()

Post by Little John »

Thade wrote:You Guys have Problems ...

What's next?
Clearing or Freeing each Variable?
x.d=1000

Oops this uses 8 Bytes of memory

FreeVariable(x.d)

:?:
:lol:
Another "extremely helpful" contribution by Mr. Thade ...
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Rename FreeArray() to ClearArray()

Post by Fred »

Little John wrote:
freak wrote:So your problem is the difference between ListSize() and ArraySize() and not between FreeList() and FreeArray(). Just because ArraySize() has a return value to indicate a freed array doesn't mean that FreeArray() does not actually free the array.
You asked for an inconsistency, and Josh showed you an inconsistency.

He probably thought (and so did I), that the results of ArraySize() and ListSize() are correct and consistent. If so, then there is an inconsistency between FreeArray() and FreeList(). Otherwise there is an inconsistency between ArraySize() and ListSize() (or a bug somewhere).
freak wrote:> You can use the freed array also as parameter in procedures.

You can do the same with a list.
After having tested it now, I see that you are right: After calling FreeList(), the list can still be used without calling NewList. I was misguided by the documentation of FreeList() which is wrong then:
To use this list again later, NewList has to be called for it.
You can no more call AddElement() on a free'ed list, that's why NewList() is mandatory. It's still known by the compiler, so you can reference it in your code, but any access will fail (same for Map and Array).
Post Reply