Memory Management And Insecure code

Source: Internet
Author: User

* ************************************ Memory manage *************************************** *******************

 

======================================== Stack:
1. stored value type (method parameters are stored in the stack)


2. High Performance


3. the lifecycle must be nested (disadvantage)

 

The structure is a value type and is stored in the stack. Structure instantiation (new) does not allocate space in the stack

 

===================================================== Managed heaps (with traditional stack has a significant advantage over):

 

1. The reference type is stored (the value type of the object member is also stored on the stack)


2. Powerful control over the data Lifecycle


3. The process of setting up a referenced variable is more complex than that of setting up a value variable, resulting in high performance overhead.

 

======================================== Destructor (required called by the Garbage Collector):


1. Run the command before the Garbage Collector destroys the object.


2. C # The Garbage Collector cannot determine when to execute the code, and the code to be executed at a certain time cannot be placed in the destructor.


3. The implementation of C # destructor will delay the time when the object is destroyed from the memory.


4. frequent use of destructor may affect performance

 

======================================== Garbage Collector

System. gc. Collect (); // force garbage collection for all generations
System. gc. suppressfinalize (object); // The Terminator of the object (destructor) is disabled)

 

 

* ************************************ No security code ************************************** ********************

 

======================================== 1, use the unsafe keyword to write Insecure code

 

// Configuration: select the reverse key of the project-> properties-> Generate-> check to allow Insecure code

// Tag class unsafe public class student {// tag field unsafe int * page; // Tag Method unsafe void GetType (int *) {// flag the code segment unsafe {int * pabc; // declare pointer syntax }}}

 

===========================================================2, pointer syntax

Unsafe {int * pwidth, pheight; double * presult; byte * [] pbyte; // &: indicates "Get address" and converts a value data type to a pointer. For example, convert int to * Int. This operation is called the [addressing operator] // *: It indicates "Get address content" and converts a pointer to a value data type (for example, * float to float ). This operator is // called "indirect addressing operator" (sometimes called "unreferencing operator") int A = 10; // declares a value type and assigns a value of 10 int * pA to it, PB; // declare two pointers: Pa = & A; // extract the address of Value Type A and assign it to pointer PA Pb = PA; // assign the address of pointer Pa to Pb * pb = 20; // obtain the address content pointed to by Pb and assign 20 console values. writeline (a); // output 20}

 

=========================================3, forcibly convert pointer to integer type

// Only uing, long, And ulong types can be converted.

Int A = 10; int * pA, PB; Pa = & A; uint address = (uint) Pa; // forcibly convert the pointer address to an integer type Pb = (int *) address; // forcibly convert the integer type to * pb = 20; // The Pointer Points to aconsole. writeline (a); // output 20

 

=========================================4, forced replacement of pointer type

Int B = 10; int * PIA; double * PDA; Pia = & B; pda = (double *) PIA; // convert int * to double *

 

====================================== 5, void pointer (no pointer to any data type)

Int c = 10; int * PAA; PAA = & C; void * PVA = (void *) PAA; // convert viod pointer

 

=========================================6, pointer arithmetic operations (void pointer operations are not allowed)

P + x * (sizeof (t ))
P: pointer address
X: the value added.
T: type size

Int d = 10; int * PID; // if the address is 100000pid = & D; PID ++; // result: 100004 (the size of the int type is 4 bytes)

 

====================================== 7, sizeof operator (only the size of value type can be obtained)

int x = sizeof(int);

 

================================================== 8, structure pointer: pointer member access operator

① The pointer cannot only support any reference type.
② The structure cannot contain reference types

Mystruct * pstruct; mystruct MS = new mystruct (); pstruct = & Ms; // -- get the address content assigned to 10 (* pstruct ). X = 10; pstruct-> X = 10; // -- assign an address to pisint * pis1 = & (Ms. x); int * pis2 = & (pstruct-> X); // structure struct mystruct {public int x = 1; Public int y = 2 ;}

 

====================================== 9, class member pointer

Person P = new person (); fixed (int * pip1 = & (P. x), pip2 = & (P. y) {}// lifecycle of pip1 and pip2 // class person {public int X; Public int y ;}

 

======================================== Interesting experiment

Unsafe {// interesting question: Why is the value of B changed? // Answer: Because the address of I points to bint A = 10; int B = 20; int * I; I = & A; // assign the address of a to pointer II-= 1; // change the address-1 of I (equivalent to moving 4 bytes down (the size of int type is 4 bytes) * I = 30; // retrieve the content pointed to by the I pointer and assign the value to 30console. writeline (B); // output 30}

 

This article is from the "programmer's home-Hunter" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1567425

Memory Management And Insecure code

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.