Current location - Loan Platform Complete Network - Big data management - What is the use of pointers?
What is the use of pointers?

Other words do not want to say more, say more you do not understand, with what language is not important, only the accumulation of experience to a certain extent you will have to appreciate the existence of any technology has its historical origins, the biggest benefit of the pointer two points

1, big data transfer, as long as the transfer of the address is equal to the transfer of more than one data, fast

2, the transfer of the address of the function to achieve the callback, event call (this is very useful for design patterns)

Callback function, it is recommended to understand the windows message mechanism, the callback function, the callback function, the event call (this is very useful for design patterns). event call (this is very useful for design patterns)

Callback function, it is recommended to understand the windows message mechanism, callback function is the basis of multitasking, the details will not be expanded. Function address passing is a very useful technology, you can realize a lot of flexible operation, no certain work experience is unable to appreciate.

Let's take an extremely simple example, there is an int array, to traverse all the elements to find the maximum value, to write a loop, the loop in the preparation of a comparison of the size of the code, for example, if (a>b){max=a.....} If (a>b){max=a}, and finally return the maximum value, this is no problem. Now I want to add an algorithm for the minimum value, then you must write another function, the same loop body, the difference is that the code inside the loop is different, to change to if (a<b){min=a.....} , and so on a lot of algorithms, when your business has a large number of loop processing, you will find the need to write N many functions, which in addition to the function name and the loop inside the algorithm is not the same, the composition of the function is almost exactly the same. On the other hand, if I want to traverse an array of types other than int? Wouldn't I have to write a bunch of overloaded functions?

A thoughtful programmer would never accept this.

How can I reduce the amount of code, can I define the different core processing code in separate packages, and I only need to write a traversal function with a loop body? And I only need to define and pack all kinds of different algorithms, when I need to pass the address of the algorithm code to the loop function, is not it? And furthermore, wouldn't it be perfect if the function or core processing code could accept and identify arrays of different types of elements?

So the need to be able to pass the address of the function has arisen, in fact, the callback function, event response is the mechanism, the meaning of the function of the pointer is when the compiler can not determine the processing mode, or how to deal with the data of the right to decide is not in the loop, the need for event callback function or event triggering mechanism to complete the mechanism is the basis of the function of the pointer.

So the significance of the pointer is not simply to manipulate a variable so simple, its impact is quite far-reaching, to my knowledge, for the realization of polymorphism to the factory model to establish the foundation.

PS: Do not think that VB C# does not have pointers, if not VB will not be able to achieve callbacks and event response. And don't think that manipulating memory is useless, because you don't need to use it yet.

By the way, cut a code snippet, my daily work requires VBA traversal of the data table traverser of the loop part of the event triggering and handling of the unit hooked up to the mechanism.

Added to the C # of the delegate, in fact, the delegate is also the use of pointers, only C # hidden, changed the name: delegate. C# in the existence of a large number of generic classes, in fact, is the realization of an interface for a variety of calls to the needs of the so-called generic class is a variable or a function of the interface can be stored to pass a different data type, not like int such as the basic type can only store the type of integer, which reduces the amount of code, of course, the price is performance.

For example, list type, it is a generic array class, encapsulates a lot of methods/functions, which find method is the front of the demand I mentioned above, find only provides a loop traversal, to find the matching conditions need to be given in addition, so C# does not need to provide more than one overloaded function, increasing the flexibility of the code to reduce the amount of work due to FIND FIND is to find, and so the parameters are very simple, the matching conditions required to be given. The parameters are simple, the matching function is required to return a bool value, indicating whether the loop is over or not.

FIND parameters is a function of delegate/pointer, FIND has a loop from index 0 to start traversing all the elements, to obtain the elements will be called after the proc, and proc is the programmer to provide the matching code, required to return to the bool. If true, then the loop ends to return to the object found. Note that the elements of list can be any object instance.

C# to achieve a lot of programmer's needs, and vb because it is an early language, you can only write their own traverser, but as long as you understand these needs or ideas, you can achieve the same function.

The most important thing to learn programming is to learn the idea and not the code, the language and the code really does not matter, they are only used to realize your ideas of the tool. Code so many words, just want to make a point, you think you do not need should not exist things have always existed, just because of the language update encapsulation of the distortion of the more convenient. In this case, proc is a pointer and is a function pointer, C # in the declaration of a delegate is the declaration of pointers, just call different, call or code form is not the same.