2

Background:

I just came across this question and made me learn about erase statment in arrays.

I have always used the following to reset them:

Sub TestWithRedimOnly()
Dim ExampleArray() As String
    ReDim Preserve ExampleArray(1)
    ExampleArray(1) = "yo"
    MsgBox ExampleArray(1)
    ReDim ExampleArray(0) As String
    MsgBox ExampleArray(1) 'this confirms is reset!
End Sub

If I do it with erase

Sub TestWithEraseAndRedim()
Dim ExampleArray() As String
    ReDim Preserve ExampleArray(1)
    ExampleArray(1) = "yo"
    MsgBox ExampleArray(1)
    Erase ExampleArray
    MsgBox ExampleArray(1) 'this confirms is reset!
    ReDim ExampleArray(0) As String
    MsgBox ExampleArray(1) 'this confirms is reset!
End Sub

At the end, both reset the variable.

Question: Is it really worthy to use erase statement? Does it prevent memory leaking better than just the Redim.. As.. Are there any cases that you can recall about not doing it like so?

Community
  • 1
  • 1
Sgdva
  • 2,800
  • 3
  • 17
  • 28
  • 2
    One difference is that after redim a(0) you still have an allocated array with 1 index at a(0). Erase exists to destroy an array, so if thats what you want to do thats what you may as well use. – Alex K. Jun 30 '16 at 16:23
  • Is it really worth to call the erase? I mean an array with 1 index with nothing on it vs an array totally destroyed and then re build -if I want to re use the array variable- – Sgdva Jun 30 '16 at 16:26
  • `Erase `does what you intend `ReDim ExampleArray(0)` to do in your example, except it does it "better" and is shorter to type and clear in intent. Personally I can see no reason not to use it. – Alex K. Jun 30 '16 at 16:28
  • 1
    So at the end, are they "code synonyms" -in terms in reliability/speed/memory usage/intention- and are up to the coder style? – Sgdva Jun 30 '16 at 16:30
  • No, they are different in behaviour; accessing `arr(0)` would throw an error after `Erase` but not after `ReDim` – Alex K. Jun 30 '16 at 16:34
  • You are right, since the other one would not have even index 0 to start with. For practical purposes, is really a difference of "resetting" with index 0 the array than with erase that completely cleans the array? – Sgdva Jun 30 '16 at 16:46
  • Why would not using *either* of them leak memory? They'll get garbage collected when they leave scope regardless. – Comintern Jun 30 '16 at 17:29
  • Yeah I use one of them, just were wondering which one was the best and why – Sgdva Jun 30 '16 at 19:02

2 Answers2

6

Since you asked about the consequences of Redim vs Erase specifically in regards to memory... The only practical difference is with dynamic arrays.

If you erase a non-dynamic array it sets the values in it to 0s, zero length strings, or "empty" for variants. eg: Dim Array(10)

If you erase a dynamic array it completely frees the memory. eg: Dim Array()

Neither option should result in a memory leak. Both should cause memory to free up in a way that doesn't constantly expand the memory used if you loop through the array and redim/erase array parts of your code. If you are running into a memory leak looping through code, that might be a better question to ask about.

From the help on erase:

Dim NumArray(10) As Integer ' Integer array.
Erase NumArray ' Each element set to 0.

Dim StrVarArray(10) As String ' Variable-string array.
Erase StrVarArray ' Each element set to zero-length string ("").    

Dim StrFixArray(10) As String * 10 ' Fixed-string array.
Erase StrFixArray ' Each element set to 0.

Dim VarArray(10) As Variant ' Variant array.
Erase VarArray ' Each element set to Empty.

Dim DynamicArray() As Integer ' Dynamic array.
ReDim DynamicArray(10) ' Allocate storage space.
Erase DynamicArray ' Free memory used by array.
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Rodger
  • 845
  • 9
  • 21
  • Yeah `ReDim NumArray(10)` allocates the memory for an array index 10 -or 11 based on base- (hence being different from resetting the variable),but, if I do `ReDim ArrayDesired(0) as Integer` instead of `Erase NumArray` what's the difference? – Sgdva Jun 30 '16 at 16:43
  • 1
    It is a nominal difference at best. If you do Dim arr() ReDim arr(0) Erase arr() and step through you will see that a redim(0) is still an array of 1. An erase arr() empties it entirely. – Rodger Jun 30 '16 at 17:27
4

When in doubt, go with the semantics:

  • Use ReDim to re-dimension an array
  • Use Erase to deallocate an array

Sticking to that will make your code benefit from greater consistency and will make your code's intent clearer to the reader/maintainer/future-you.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
  • I guess this is a good advice, even though one may behave similar to the other one, for standardized purposes, Erase would be a better way to go. – Sgdva Jun 30 '16 at 17:01