What memory semantics governs array assignment in C #?

Given the following: byte [] sData; and a function declared as private byte [] construct_command ()

if I then assigned the result construct_command () sData strong> would sData point to the contents of what was returned from the function, or would some space be allocated for sData in memory, and the contents of the result of the function would be copied to it?

+4
source share
7 answers

Assuming sData is a local variable, it will be on the stack and will reference the array returned by this method. The method does not return the contents of the array itself, but a reference to the array.

In .net, arrays are objects of the first class, and all variables of the array type are actually references.

+2
source

The assignment simply assigns sData a reference to the instance returned by the construct_command command. Copying data will not happen.

In general, the CLR breaks the world into 2 types

  • Value types: this is all that comes from System.ValueType. Assignment between the values โ€‹โ€‹of these types occurs by value and essentially leads to the copying of values โ€‹โ€‹between locations
  • Types of links: everything else. Assigning between the values โ€‹โ€‹of these types simply causes the location to refer to another object in memory. No copying of values

Arrays are reference types in the CLR and therefore do not copy the base value.

+6
source

Arrays are reference types, this means that the actual array is created on the heap (presumably by the construct_command () command), and the function returns a reference to the array and is stored in (local variable) sData.

So, this does not concern the semantics of memory (the return value may be zero), but also the semantics of copies of reference types. The situation is completely equal, for example:

StreamReader reader = System.IO.File.OpenText(filename); 

To do this a little more frankly: you cannot transfer an array to .Net at all, you can transfer, copy and assign references to arrays.

+3
source

sData will indicate the contents of the returned function. Arrays in C # are reference types, which means that assigning one array from another simply copies the link, rather than allocating new data.

+2
source

An array is a reference type, so only the link is copied. No content manipulation.

+2
source

sData points to the array returned by construct_command .

0
source

While Rui's answer is fine, I believe some of it is incorrect. It makes no sense (extremely inconsistent) for an array that will be allocated from the stack memory and then accessible by reference. If that were the case, the underlying memory could be freed from any methods to which the link was sent.

I believe that Henk Holterman correctly described this when he said very bluntly and unequivocally:

Arrays are reference types, which means that the actual array created on the heap

Thanks to Henk for this comment; I think this is a critical part, otherwise there is no (and contradicts) the accepted answer.

(I would respond to the comment, but I do not have enough answers, and I think it is important enough to indicate that the accepted answer has this problem for me to add a new comment here.) Perhaps the administrator can remove the link to "local variable, it will live on the stack from the answer.

0
source

All Articles