Pointers to C + + and C # pee

Source: Internet
Author: User

yesterday and Zhao Chong said the work of things, said the performance of the problem on the data and the performance of the pointer to the impact of the past has not thought of this aspect of the matter, these days dedicated to take time to review this knowledge, then 1.1 points to summarize, Look at how data structures and pointers can improve efficiency in our code.

Today let's talk about pointers, about pointers, learning C + + time to contact the pointer, but at that time learning foggy, also did not have a good summary, so that forget the almost, if you have the pointer is not familiar with the place, we first to review the C + + 's hands.

in C + + , pointers are defined like this: A pointer is a special variable, and the value stored in it is interpreted as an address in memory. To make sense of a pointer, you need to be aware of the four aspects of the pointer: the type of pointer, the type that the pointer points to, the value of the pointer, the memory area that the pointer points to, and the memory area occupied by the pointer itself. Let's explain separately.

int *ptr;  char *ptr;  int **ptr;  int (*PTR) [3];  int * (*PTR) [4];

Through the above example, we put the pointer to the declaration syntax, the rest is the pointer, so the pointer above is "ptr".

Then let's look at the type of pointer:

int *ptr; The type of the pointer is int *  char *ptr;//The type of pointer is char *  int **ptr;//The type of pointer is int * *  int (*PTR) [3];//The type of pointer is int (*) [3]  int * (*PTR) [4]; The type of pointer is int * (*) [4]  

What do you think? Is it easy to find out the type of pointers?

The type that the pointer points to

When you use a pointer to access the memory area pointed to by the pointer, the type that the pointer points to determines what the compiler will look for in that memory area.

The type that the pointer points to

int *ptr; The type of pointer is int char *ptr; The type of pointer is char int **ptr; The type of pointer is int *int (*PTR) [3]; The type of pointer is int () [3]  int * (*PTR) [4];//The type of pointer is int * () [4]  

The value of the pointer

This needs to look at the operating system, if our system is a three-bit, then our pointer is a value of the length of the number, because the computer's memory length is a three-bit, similarly, the 64 the value of the bit length, of course , this is both 0 and 1 , because the computer can only know 0 and 1.

The memory area that the pointer points to is the memory address from which the value of the pointer is represented, and the length of the memory area of sizeof ( the type the pointer is pointing to ) . Later, we say that the value of a pointer is XX, which is equivalent to saying that the pointer to an XX -led address of a piece of memory area; we say that a pointer points to a region of memory, which is equivalent to saying that the value of the pointer is the first address of the memory area.

Operators & and *

Here & is the fetch address operator,* is ... The book is called the "indirect operator." The result of the &a operation is a pointer, the type of the pointer is a type of a (for example, int*), the type of the pointer is the type of a (int the address that the pointer points to, which is the address of a . The result of *p 's operation is very multifarious. In short *p The result is what p points to, this thing has these characteristics: its type is the type of p , which occupies the address that p points to.

Said to turn over, is a our mobile phone navigation process,a is your home,&a is pointing to your home address, such as P=&a, then P is my home address, then *p * is the equivalent of our mobile phone navigation, through the address you entered, to find your home.

The above example, simply say what is called the address, then if you want a deeper understanding of the pointer, we recommend a blog, write a very basic http://www.cnblogs.com/basilwang/archive/ 2010/09/20/1831493.html

So why do you have pointers, if not you design a function

struct Get () {

.........

}

Return a function of a struct object, you know, inC + + , such a return value is the process of copy passing, that is, when you return to the struct, the program will copy the same struct object in the stack, and then accept the variable in the copy constructor, copy a new variable. The final procedure is to deconstruct this temporary. If the structure is small, no problem, if it is large? Such a construct, a destructor, can be a waste of time. But the pointer is different, how you do it, anyway, is 4 bytes, and an int , no difference at all.

So let's talk about the " Evolutionary History " of C + + to C # , and the code we see doesn't seem to be using pointers like C + + code anymore. Later people say Microsoft is not there is no pointer, in fact, Microsoft is some, we right-click on our solution under the class library -à generate an unsafe code, we tick Ah, the same can be used, but write classes and methods when the front plus an unsafe. For example:

Public partial unsafe class demostatic unsafe void Copy (byte[] src, int srcindex,            byte[] DST, int dstindex, int count)        {//..... }

Another key to note, that is Fixed, his role is a nail, you see the above introduction will find that the pointer is actually the computer 0 and 1. The pointer also takes up memory, except that his size is fixed. The fixed statement can be used to set a pointer to a managed variable and "pin" the variable during statement execution. If there is no fixed statement, the address of the pointer to the moveable managed variable is variable, because garbage collection may relocate the variable unpredictably.

The C # compiler only allows pointers to managed variables to be assigned in the fixed statement, but cannot modify the pointers initialized in the fixed statement.

Pointers can be initialized with the address of an array or string:

Fixed (int* p = arr)  ... Equivalent to P = &arr[0] fixed (char* p = str) ...//equivalent to P = &str[0]

Below is a complete example of a C # use pointer

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace Cursortest{class Program {//Use unsafe to label the method static unsafe void Copy (byte[] src, int srcind                Ex, byte[] DST, int dstindex, int count) {if (src = = NULL | | Srcindex < 0 | | DST = = NULL | | Dstindex < 0 | |            Count < 0) {throw new ArgumentException (); } int srclen = src.            Length; int Dstlen = DST.            Length;                if (Srclen-srcindex < count | |            Dstlen-dstindex < count) {throw new ArgumentException ();  }//fixed pinning the pointer without letting him change the fixed (byte* PSRC = src, pDst = DST) {byte* PS                = PSRC;                byte* PD = pDst; Cyclic replication for (int n = 0; n < count/4; n++) {* ((int*) PD)= * ((int*) PS);                    PD + = 4;                PS + = 4; }//completion assignment for (int n = 0; n < count% 4; n++) {*PD = *p                    S                    pd++;                ps++;            }}}} static void Main (string[] args) {byte[] a = new byte[100];            Byte[] B = new byte[100];            for (int i = 0; i < ++i) a[i] = (byte) i;            Copy (A, 0, b, 0, 100);            Console.WriteLine ("The first elements is:");            for (int i = 0; i < ++i) Console.Write (B[i] + "");            Console.WriteLine ("\ n");        Console.ReadLine (); }    }}

But why doesn't our code use pointers now, because in the common language runtime (CLR) , unsafe code refers to code that cannot be verified. Unsafe code in C # is not necessarily dangerous, but code whose security cannot be validated by the CLR . Therefore, theCLR only performs operations on unsafe code in fully trusted assemblies. If you use unsafe code, it is your responsibility to ensure that your code does not cause a security risk or a pointer error , so if you are very assured of your code, then it is no problem.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Pointers to C + + and C # pee

Related Article

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.