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 discussed the data results and the impact of pointers on performance. Have never thought of this aspect of the matter, these days specifically to take time to recall this knowledge, and then a summary of 1.1 points to see how the data structure and pointers in our code to achieve the improvement of efficiency.

Let's talk about pointers today. For pointers, when you are learning C + + , you are exposed to pointers. But at that time learning foggy, also did not have a good summary, so that almost the same forgotten, assuming that we also have a pointer unfamiliar place. Let's think about C + + pointers first.

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 understand the four aspects of the pointer: the type of the pointer, the type that the pointer points to, the value of the pointer, or the memory area that the pointer points to. There is also 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 example above. Everybody put the pointer to the Declaration grammar. 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 the method of finding the type of pointer very easy?

The type that the pointer points to

When you use the pointer to access the memory area pointed to by the pointer, the type that the pointer points to determines how the compiler will treat the content 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, assuming that 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 the first. In the same way, the magnitude of the value is the length of the size , of course, this and three. are expressed in 0 and 1 , since the computer can only know 0 and 1.

The memory area pointed to by the pointer starts at the memory address represented by the value of the pointer, and the length is a memory area of sizeof ( the type the pointer is pointing to ) .

Later, we say that the value of a pointer is XX. It is equivalent to saying that the pointer points to an area of memory with an XX -headed address. We say that a pointer points to an area of memory. It is equivalent to saying that the value of the pointer is the first address of this area of memory.

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 the type of a plus a *(for example, int*). The type that the pointer points to is the type of a (int). The address that the pointer points to. That's the address of a .

The result of *p 's operation is very multifarious.

In short, the result of *p is what p points to. This thing has these characteristics: its type is the type that p points to. The address it occupies is the address that p points to.

Said to turn over. is a navigation process for our mobile phone. 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. Find your home by the address you entered.

The above example simply says what is called an address. So suppose you want a deeper understanding of pointers. 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 () {

.........

}

Returns a function of a struct object, you know, inC + + , this return value is the process of copy passing. In other words, when you return to the struct, the program copies the same struct object in the stack, and then accepts the variable in the copy constructor, copying a new variable.

The final procedure is to deconstruct this temporary.

Suppose the structure is very small. No problem, suppose it is very big? Such a construct, a destructor, can be a waste of time. But the pointer is not the same, the tube you how to do, anyway, is 4 bytes. As with an int , there is no difference.

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 that Microsoft has no pointers. In fact, Microsoft is there, everyone right-click on our solution to the class library-à generate a non -security code, everyone tick Ah, the same can be used. Just write classes and methods and add an unsafeto the front. Like what:

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

Another keyword to note is that it is Fixed. His role is a nail, you see the above introduction will find that the pointer is actually 0 and 1of the computer.

Pointers also consume memory. It's just that his size is fixed.

The fixed statement can be used to set a pointer to a managed variable and "pin" the variable while the statement is running. Assume there is no fixed statement. The address of a pointer to a movable managed variable is variable. Because garbage collection may relocate variables unpredictably.

The C # compiler simply agrees to assign a pointer to a managed variable in the fixed statement, but cannot alter the pointer initialized in the fixed statement.

The ability to initialize pointers 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 shows you a complete sample, a sample of C # using pointers

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 (); }//Pinning the pointer with a fixed.                Don't let 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 today, because in the common Language execution Library (CLR) , unsafe code refers to code that cannot be verified. Unsafe code in C # is not necessarily critical, it is just code whose security cannot be validated by the CLR .

So. The CLR only runs operations on unsafe code in fully trusted assemblies. Assume that unsafe code is used. It is your responsibility to ensure that your code does not cause security risks or pointer errors . So if you're assuming that your code is guaranteed, it's okay to use it.



Pointers to C + + and C # pee

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.