1

What will be the memory address of arr, when I am doing arr = new T[size] twice in the same scope while varying size?
Does the memory expands to new size and starting address of arr remains the same?
Or if the new memory allocation happens then the previous data get copied to new memory location?

using System;
class HelloWorld
{
    static void Main ()
    {
        byte[] arr;
        arr = new byte[6];
        arr[2] = 6;
        Console.WriteLine ("len = {0}\n{1}", arr.Length, arr[2]);
        arr = new byte[10];
        arr[2] = 10;
        Console.WriteLine ("len = {0}\n{1}", arr.Length, arr[2]);

    }
}
11
  • 1
    new byte[10] allocates a new memory chunk, arr is a reference to this chunk; byte[6] is now garbage which can be collected by GC (garbage collector) Commented Dec 1, 2021 at 9:25
  • You're writing managed (garbage collected) code and you have a compiler that may optimize this to Console.WriteLine("len = 6\n6"); Console.WriteLine("len = 10\n10") anyway. Asking questions like this could give interesting answers about memory management and compiler optimizations, but what have you tried? Commented Dec 1, 2021 at 9:25
  • does the memory expands to new size - it doesn't matter/it's an implementation detail that is largely irrelevant. What is specified is that when you make a new array it will be full of 0, so there is no copying of data to any place; you'll never have an arr= someIntArrayOf6Length and then do arr = new int[10] and ever get any of the original data in your new 10-long array Commented Dec 1, 2021 at 9:26
  • 1
    put Array.Resize(ref arr, 10); if you want arr be created from previous byte[6] array (previous data will be copied) Commented Dec 1, 2021 at 9:29
  • 1
    I wanted to check re-creation of the arr gives compile time error or not - you didn't need to post a question on SO to ascertain that; you've already written the code - does it compile or not? Commented Dec 1, 2021 at 9:31

2 Answers 2

2

According to your coding part first you have initialized the array as

arr = new byte[6];

When you again intializing the array as

 arr = new byte[10];

The array will be allocated with a size of 10 and filled with the default value (here 0). There is no data copy.
And also the byte [6] will eventually be collected by the garbage collector.

To keep the data in a resized array, you can use Array.Resize as explained here, but it will change the reference:

var arr = new byte[6];
var arr2 = arr;

Array.Resize(ref arr, 10);
// arr != arr2
Sign up to request clarification or add additional context in comments.

1 Comment

Even though it looks like this is the current implementation, there is no guaranty about this in the future : github.com/dotnet/runtime/issues/4584
2

new byte[6] and new byte[10] allocate new, different, memory chunks. That is, they will (probably, as mentioned in the comments) not be in the same location in memory.

arr is not the array itself. It is simply a variable that can hold a reference (the address) to a byte array. When you are reassigning it, you are simply updating its value to contain a reference to the new memory chunk, while the other one is collected by the Garbage Collector.

7 Comments

Can you clarify what "different" means?
Even though it looks like this is the current implementation, there is no guaranty about this in the future : github.com/dotnet/runtime/issues/4584
@Orace I think something like "There is a GitHub issue where there is discussion about 'putting small objects that never leave the method onto the stack', so somewhere in the future, might that issue be implemented, both arrays may occupy the same stack frame, the first having been popped off" would have immediately been clearer, but thanks for clarifying.
@MichaelHaddad yes, for optimization purpose.
@CodeCaster thanks for the clearer phrasing, like Michael, I'm not a native English speaker.
|

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.