c++/c# stack, object memory model, depth copy, Array.clone method

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/jarvischu/article/details/6425534

Directory

1. Object memory model in c++/c# ..................................................................................................... 1

1.1. Stack memory and heap memory ...................................................................................................... 1

1.1.1. Stack memory ........................................................................................................... 1

1.1.2. Heap Memory ........................................................................................................... 1

1.1.3. Stack memory compared to heap memory ...... ..... ..... ................ ....... ..... .................. ......... 1

1.2. Value types and reference types ................................................................................................... 2

1.3. Memory model  .................................................................................................................. 3

2. Deep copy and shallow copy .................................................................................................................. 4

2.1. Case 1 ......................................................................................................................... 4

2.2. Understanding ......................................................................................................................... . 5

2.3. Case 2  ........................................................................................................................ 6

3. Clone of System.Array () ........................................................................................................ 8

1. Object memory model in c++/c# 1.1. Stack memory and heap memory stack memory 1.1.1. Stack memory

• Automatically allocated and freed by the compiler

L is used to save some local variables, function parameters, etc.

Understand:

l int A;

The compiler will automatically allocate a sizeof (int) size memory to variable a in the stack.

1.1.2. Heap Memory

• Manual application by programmer and [release]

In C + +, the compiler is not freed by the new application and must be released through delete.

In C #, with the new application, programmers omit the delete operation due to the compiler's GC (garbage collection Mechanism)

L char* p = new char[10];//c++

Char[] p = new char[10];//c#

First, the compiler allocates a stack memory save variable p

After that, the compiler allocates a heap of memory with a size of sizeof (char) *10

Finally, the first address of the heap memory is stored in p.

1.1.3. Stack memory compared to heap memory

U memory allocation

Stack: LIFO; The compiler assigns the appropriate type size; The allocated size is limited by the size of the stack

Heap: Arbitrarily assigned, manually requested by the programmer to specify the size; the allocated size is limited by the computer's virtual memory

U efficiency

Stack: High

Heap: Low relative stack

1.2. Value types and reference types

U Simple Understanding

A value type variable holds the actual data, referring to the memory address where the actual data is stored by the type variable.

U Storage Differences

Value type data is stored in the stack memory, the actual data corresponding to the reference type variable is stored in the heap memory (the variable itself is stored in the stack memory, usually four bytes, holding an address value)

U C + + type

Value type: Int,float,double,char,enum

Reference type: Array,structure,class

U C # type

Value type: Struct,enum

Reference type: Class,delegate,array,interface

Why is there no such type of int,float? (Write a leak?) )

To answer this question, take a look at MSDN:

Types the define by using the struct keyword is value Types; All the built-in numeric types is structs. (C #)

Here are three points to emphasize:

Üstruct (struct) type is a value type

Übuilt-in numberic types (built-in numeric type, that is, int,float, etc.) are structs, such as int corresponds to the SYSTEM.INT32 structure

ü With MSDN record, System.Int32 is the alias of int

Figure 1 Overview of types in C #

1.3. Memory model

Figure 2 Value type memory model

Figure 3 Reference type memory model (C # For example, C + + similar)

2. Deep copy and light copy 2.1. Case 1

Take the person class in Figure 3 as an example. (Name and age are the public properties of the person Class)

What is the result of the above statement execution?

This involves deep and shallow copies, and after understanding the two concepts, the answer to the question comes out.

2.2. Understanding

The copy function is executed when object A is assigned to another object B.

Deep copy: A copy of A is completely copied and assigned to B (copy value) (can be understood as a duplicate)

Shallow copy: Copy a reference to a, then assign to B (copy Reference) (can be interpreted as an alias)

In Figure 4:

Person B = al;

Shallow copy, which simply assigns the reference to object A (that is, the variable a, or, more specifically, the memory address stored in a) to the variable B. In this way, A and B point to the same piece of memory.

Person C = new person (a.name,a.age);

Deep copy, which copies the reference to object A and the memory area it points to, and assigns a value to B. In this way, both A and B point to memory areas with different addresses but with the same data.

So, the answer to the above question is also out, output:

2013-10-23 Revision

Chu

Jarvis

Figure 4 Shades of a copy

2.3. Case 2

OK, let's take the strike and watch another situation.

The country class and the person class are defined as follows:

What is the result of the following code output?

[CSharp]View Plain Copy
    1. Country Chinese = new Country ("China", 960);
    2. Person A = new person ("Jarvis", 22,china);
    3. Person B = A;
    4. Console.WriteLine (A.motherland.countryname);
    5. Console.WriteLine (B.motherland.countryname);
    6. China. CountryName = "Republic";
    7. Console.WriteLine (A.motherland.countryname);
    8. Console.WriteLine (B.motherland.countryname);

In this case, two shallow copies were involved,

Once is Motherland = land; ,

Once is person B = A;.

It embodies the following:

Assigns an object (a) that contains an object Type property (Motherland) directly to another object (b).

So what is the process of this copy (obviously a shallow copy)? What is the state of the in-memory data?

The answer is:

Republic

Republic

(Are you correct?) )

Take a look at my diagram.

Figure 5 Copy with object properties

If the 2 "China" became the "Republic", of course, the output is two lines of "republic"

3. System.Array's Clone () method

Why do you want to talk about this clone () method specifically? What's so special about it?

It might be a little special, but it's not the main reason, but it's the way I've been struggling for a long time because MSDN says it's a shallow copy. But the result of the following code output is different from what I thought:

[CSharp]View Plain Copy
    1. Int[] Arroriginal = {1, 2, 3, 4};
    2. int[] Arrcopydirect = arroriginal; Direct Shallow copy
    3. Int[] Arrcopyclone = (int[]) arroriginal.clone (); Clone method
    4. Showarray (arroriginal); The Showarray method is simply to display each element of the array showarray (Arrcopydirect);
    5. Showarray (Arrcopyclone);
    6. Arroriginal[0] = 0;
    7. Console.WriteLine ();
    8. Showarray (arroriginal);
    9. Showarray (Arrcopydirect);
    10. Showarray (Arrcopyclone);

The result of the output is:

The Clone method is to copy the elements in the arroriginal array to the Arrcopyclone array, so after the assignment ends, the value of the element of the arroriginal array is changed (arroriginal[0] = 0), Arrcopyclone will not be affected (arrcopyclone[0] = 1), is this a shallow copy?

A bit confusing.

However, if you test with an array of person class type instead of an array of type int, you can see that if the source array A is assigned to a new array B, the elements in a change, and B changes accordingly.

It took me a long time to understand, and in the process, I made a further understanding of some of the points mentioned above.

I finally found the answer, and the MSDN explanation is:

A shallow copy of an array copies only the elements of the Array, whether they is reference types or value types, but it Does not copy the objects, the references refer to. The references in the new array, point to the same objects, the references in the original array.

It means:

This method copies all the elements of an array to another array, regardless of whether the element in the array is a value type (value Types, such as int) or a reference type (Reference Types, such as person).

The result is that if the elements in the original array are value types, this is equivalent to a deep copy, which is equivalent to a shallow copy if the elements in the original array are reference types.

So I think it's debatable to say that it's shallow copy directly on MSDN.

c++/c# stack, object memory model, depth copy, Array.clone method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.