5

I was working with my project in Visual studio in c#, but one thing I don't know about is that when we reallocate a array (no matter what type it is of) then does the previous values that stored in the array are destroyed or they are not? For Example:

int[] a=new int[2];
a[0]=200;
a[1]=400;

Now I reallocated the array 'a' with 4 elements:

a=new int[4];

Now, does the previous values will be there or they changed to something new value, i mean garbage, zero or they will be as they was? I also tried it myself in visual studio, and value didn't changed, but I want to be sure if really they don't.

1
  • Array.Resize(ref a, 4); when a = new int[4]; creates a new array with all items equals to default(int), 0 in your case Commented Feb 28, 2017 at 11:31

4 Answers 4

4

Doing this:

a = new int[4];

you set a to point to another array in another memory region.

If there is no another references to old array it will be in memory until next garbage collection clears it.

Sign up to request clarification or add additional context in comments.

3 Comments

so, if i use "Array.Resize(a, 4)" then what?
As source said This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.array must be a one-dimensional array. you simply creates new array with new size and copy all elements so, the result will be the same. msdn.microsoft.com/en-us/library/bb348051(v=vs.110).aspx
@jimmy: arrays are fixed sized, you can't change their size without creating a new array and re-assign that to the old variable. So the old array is ready to be garbage collected because it isn't referenced anymore. You don't use Array.Resize in your code, you are basiacally doing the same by creating a new array with the new size. If the array stored reference types instead of integers the question would be more interesting because it's possible that they stay in memory if they are referenced somewhere else.
2

When you have any other references to the former array it still stays in memory.

int[] a=new int[2];
a[0]=200;
a[1]=400;

int[] additionalReference = a; // The array stays in memory 
a = new int[4];

Otherwise the garbage collector handles the array (see Fundamentals of Garbage Collection).

Check the documentation of Array.resize:

This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.array must be a one-dimensional array.

2 Comments

so, if i use "Array.Resize(a, 4)" then previous values are preserved or they removed?
it is preserved, when another reference to the array exists - otherwise garbage collector.
1

When you write this line

a=new int[4];

a new array is created. a[0] will be 0. If you want to resize the array use

Array.Resize(ref a,4);

This will retain the original values and resize the array.

Comments

1

In order to understand what happens, you first have to understand what variables really are. A variable is a placeholder for a value. And what is that value? For value types, the value is the instance of the value type itself, for reference types, the value is simply the memory address, so to speak, of the object its referencing.

So, that said, when you do the following:

int[] a = new int[2];

Because arrays are reference types, the value stored in a is the address of the newly created array.

Then when you do:

a = new int[4];

The only thing you are doing is overwrting the value stored in a, which we've alread said is simply the address of the object its referencing.

What happens to the original array? Absolutely nothing, it lives on blissfully happy not knowing you've just reassigned a reference that was pointing to it. If no other live references exist to the original array, then the GC could, if it so decided, collect it, but when that happens is entirely up to the GC (it might not even happen at all in the lifetime of your application).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.