In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what is the memory management and unsafe code in c#. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.
* * memory Management * *
= = stack:
1, the value type is stored (the parameters of the method are stored in the stack)
2. High performance
3, the life cycle must be nested (disadvantages)
Structure is a value type and is stored in the stack. Structure instantiation new does not allocate space in the stack
= = managed heap (which has a significant advantage over traditional heaps):
1, the reference type is stored (the value type of the object member is also stored on the heap)
2. Powerful control over the life cycle of the data
3. The process of establishing reference variables is more complex than that of establishing value variables, and the performance overhead is high.
= = destructor (must be called by garbage collector):
1, execute before the garbage collector destroys the object
2Jet c # cannot determine when the garbage collector will execute and cannot place code that needs to be executed at a certain time in the destructor
3The implementation of the c# destructor delays the time for objects to be destroyed from memory.
4. Frequent use of destructors will affect performance
= = garbage collector
System.GC.Collect (); / / force garbage collection on all generations
System.GC.SuppressFinalize (object); / / do not deactivate the object's finalizer (destructor)
* * unsafe code * *
= = 1, write unsafe code with the unsafe keyword
/ / configuration: select Project Anti-Keys-> Properties-> build-> check allow unsafe code
/ / tag class unsafe public class Student {/ / tag field unsafe int* pAge; / / tag method unsafe void getType (int* a) {/ / tag code snippet unsafe {int* pAbc; / / declare pointer syntax}
= = 2, syntax of the pointer
Unsafe {int* pWidth, pHeight; double* pResult; byte* [] pByte; / / &: means "take address" and convert a value data type to a pointer, for example, int to * int. This operation is called the [addressing operator] / / *: means "get address content" and converts a pointer to a value data type (for example: * float to float). This operator is called the "indirect addressing operator" (sometimes called the "dereferencing operator") int a = 10 int* pA / declares a value type, assigns it a value of 10 int* pA, and pB;// declares two pointers pA = & a / / take out the address of the value type a, assign it to the pointer pA pB = pA;//, assign the address of the pointer pA to pB * pB = 20 bang / get the address content pointed to by pB, and assign a value of 20 Console.WriteLine (a); / / output 20}
= = 3, force the pointer to an integer type
/ / can only be converted to uing, long, ulong types
Int a = 10intint * pA, pB;pA = & uint uint address = (uint) pA;// cast the pointer address to integer type pB = (int*) address;// cast integer type to pointer * pB = 20 pB / pointer to aConsole.WriteLine (a); / / output 20
= = 4, forced replacement of pointer type
Int b = 10intt * pIa;double* pDa;pIa = & bter PDA = (double*) pIa;// forcibly converts int* to double*
= 5ther void pointer (pointer not pointing to any data type)
Int c = 10 intt * pAA;pAA = & cbot Void * pVa = (void*) pAA;// convert viod pointer
= = 6, pointer arithmetic operation (void pointer operation is not allowed)
Pairx* (sizeof (T))
P: address of the pointer
X: added value
T: size of the type
Int d = 10 intt * pId;// if the address is 100000pId = & d _ locpId _ intact _ pId;// / result: 100004 (the size of the int type is 4 bytes)
= = 7 moment sizeof operator (only the size of the value type can be found)
Int x = sizeof (int)
= = 8, structure pointer: pointer member access operator
① pointers cannot only think of any reference type
Reference types cannot be included in ② structures
MyStruct* pStruct;MyStruct ms = new MyStruct (); pStruct = & ms;//-- takes the address content assignment as 10 (* pStruct). X = 10 struct MyStruct pStruct-> X = 10 Mather hand hand-take the address assignment 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)) {} / / pIp1 and pIp2 lifecycle / / class class Person {public int X; public int Y;}
An interesting experiment
Unsafe {/ / interesting question: why the value of b has changed / / answer: because the address of I points to bint a = 10 leading int b = 20 intt * I * * I = & a * I / assign the address of a to the pointer ii-= 1 Span / the address of I-1 (equivalent to moving down 4 bytes (the size of the int type is 4 bytes) * I = 30X / take out what the pointer points to as 30Console.WriteLine (b) / / output 30} Thank you for reading this article carefully. I hope the article "what is memory Management and unsafe Code in c#" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.