Difference between constant definition and field Definition

Source: Internet
Author: User
Tags constant definition

Essentially, no memory allocation is required for constants during runtime, but dynamic memory is allocated for fields when the instance where the field is located is created.

There are several issues that need to be discovered in depth,

First, it is precisely because the constant does not have the relevant memory address that the compiler will determine during compilation that all the values on the constant symbol are replaced. Asp.net may cause version Problems Based on this mechanism, for example:

IfProgramThe constant in Set B has changed due to business factors, so assembly B must be re-compiled. However, if other assembly that referenced the assembly B constant previously had to be re-compiled.

Otherwise, the constant value will always use the previous value. This is the version issue.

Second, when the field will be allocated memory,

In the case of instance fields, when a machine instruction named newobj in the intermediate language instruction is compiled by JIT to generate the machine instruction, the CLR will calculate the space required for all fields and

Allocate the corresponding space in the managed heap. Many languages have operators such as new, such as C #, which compile and generate il commands such as newobj.

There is another situation,When the deserialization object and memberwiseclone are used as object copies, the memory is allocated and the members of the object are initialized.CodeThis is because the C ++ code of the kernel is called directly. It may only be a copy of bytes.

For a type field, that is, a static field, I personally think that the type will be initialized before the type is accessed, that is, the caller Type constructor, at this time, the memory will be allocated, which may be before.

 

Introduction:

Note: Some methods, such as system. objet. memberwiseclone (), are labeled with [methodimplattritions (methodimploptions. internalcall)] In Asp.net. This part of code is invisible using the Il tool.

Internal callend up making a call to a C ++ function in the CLR. You can find them back inRotor source code. Look at CLR \ SRC \ VM \ eCall. cpp to find the mapping from the. NET visible name to the CLR function name. Beware that the source is getting dated. 

 (For personal opinion, please correct me. Thank you .)

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.