Using pointers in C #

Source: Internet
Author: User
Tags count int size
The pointer in the c\c++ is a good thing, but to the java,.net of the Times the pointer has been encapsulated, not visible to users, this Java does very thorough.. NET because there is also a managed C + +, the pointer is not completely abolished, C # still retains the operation of the pointer.
To use the pointer first to declare with unsafe the code that uses the pointer, as with the public declaration, you can declare the entire class, or it can be a method or property inside the class. After what's in the code, you need to modify the project's Build property to allow the compiler to support the operation of the pointer.
You can use pointers if you do a good job beforehand. There is not much difference between the use of pointers and the use of C + +. As long as the compiler does not complain, there is not much problem.
The following is an understanding of some of the use of pointers:
1. The pointer type can be either an entity variable (int,double) or an enum, and a struct variable struct is also supported. But it cannot be a class. However, a null pointer can point to a class, except that the null pointer cannot do anything, and can only use the null pointer as a pass-through object.
2. C # provides a keyword stackalloc used to request stack memory. Note that the memory allocated for this application is stack memory, which is automatically recycled when the function completes. However, I would like to use this stack of memory can basically solve the problem of 40%, and use the time without worrying about the memory leak problem.
3.. NET does not seem to support the application of heap memory directly (this is dangerous for. net), but we can apply by invoking the Win32 API method. This will solve the remaining 40% problem. Heap memory Application method in MSDN has the relevant documentation, the implementation code see attached 1.
4. The structure body is a special object. He defines a keyword with a class, and uses the same method as a class to define a property and define a method. But there's a big difference between the two when you're working on the pointer. The structure can be sized by sizeof (), and the size is related to the number of entity variables in the structure, but if the object of the class is defined in struck, or the pointer, sizeof may compile (void* null pointer exception, but need to add unsafe to the struct declaration).
5. Fixed keyword: there is not much to know at this time, but there is a useful example to allow pointers to interoperate with arrays in. NET:

byte[] buffer = new BYTE[100];
Fixed (byte* p = buffer)
{
P[0] = 123;
......
}

6. Other
7.




Appendix 1:
Public unsafe class Memory
{
Handle for the process heap. This handle are used in all calls to the
Heapxxx APIs in the methods below.
static int ph = GetProcessHeap ();
Private instance constructor to prevent instantiation.
Private Memory () {}
Allocates a memory block of the given size. The allocated memory is
automatically initialized to zero.
public static void* Alloc (int size)
{
void* result = HeapAlloc (ph, heap_zero_memory, size);
if (result = = null) throw new OutOfMemoryException ();
return result;
}
Copies count bytes from Src to DST. The source and destination
Blocks are permitted to overlap.
public static void Copy (void* src, void* dst, int count)
{
byte* PS = (byte*) src;
byte* PD = (byte*) DST;
if (PS > PD)
{
for (; Count!= 0; count--) *pd++ = *ps++;
}
else if (PS < PD)
{
for (PS = count, pd + = count, Count!= 0; count--) *--pd = *--ps;
}
}
Frees a memory block.
public static void Free (void* block)
{
if (! HeapFree (ph, 0, block)) throw new InvalidOperationException ();
}
Re-allocates a memory block. If the reallocation request is for a
Larger size, the additional region of memory is automatically
Initialized to zero.
public static void* ReAlloc (void* block, int size)
{
void* result = HeapReAlloc (ph, heap_zero_memory, block, size);
if (result = = null) throw new OutOfMemoryException ();
return result;
}
Returns the size of a memory block.
public static int SizeOf (void* block)
{
int result = heapsize (ph, 0, block);
if (result = = 1) throw new InvalidOperationException ();
return result;
}
Heap API Flags
const int heap_zero_memory = 0x00000008;
Heap API functions
[DllImport ("kernel32")]
static extern int GetProcessHeap ();
[DllImport ("kernel32")]
static extern void* HeapAlloc (int hheap, int flags, int size);
[DllImport ("kernel32")]
static extern bool HeapFree (int hheap, int flags, void* block);
[DllImport ("kernel32")]
static extern void* heaprealloc (int hheap, int flags,
void* block, int size);
[DllImport ("kernel32")]
static extern int heapsize (int hheap, int flags, void* block);
}




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.