Experience programming Great God: "The essence and C language pointer"!

       Many programming languages have " no pointer " to promote their advantage, however, for the C language, the pointer is born.

01、

So, what is a pointer? Why do we all want to avoid pointer?

       Very simple, the pointer is the address, when there is an address as a variable, it is called a pointer type of the variable, so naturally it is a pointer type.

       Pointer action is, given a pointer, the pointer to the value taken at the address. In order to understand the nature, we start with a computer model.

       Macro view, the computer can be divided into two categories:

☄ storage - execution of a computer. Typical examples of such machines is the computer we usually use, a the CPU, a memory, CPU comprises an arithmetic logic only, all instructions and data in memory, only the memory storage, computation does not contain any components.

☄ site programming computer. Typical examples of such machines is a circuit ASIC, FPGA this. Logic circuits built directly for specific needs, however, due to problems with Cartesian product, not suitable for general purpose computing.

       We look store we usually use - computer mode of implementation of the model:

☀  the CPU to transmit a memory address on the address bus.

☀  memory address corresponding to the specific return data to the data bus.

       It appears that general-purpose computer is to complete all the work by the pointer. CPU does not have the ability to directly manipulate the value of memory, it must do the following operations to detour:

§   taken from a specific address value V0 A0.

§   to generate a arithmetic processing V0 V1.

§   V1 is stored in a specific address A1.

       Initially, it is in accordance with the above logic of such a program, which is the assembly language:

       However, this is too much trouble, C language with a simple generic UNIX operating system was born, the following statement appears to be more convenient:

       C language works directly mapped CPU, and is an extremely simple way, which is the art of the C language.

       This is the background C pointer. In those days, people did not want the computer to help complete more complex business logic, people just want to use a more simple way to abstract the behavior of the computer, the final crystallization is the C language.

       So we said, the essence is the C language pointer, pointers are all C language . We can not if-else language, we can not switch-case statement, we can do while, we do not for, but we must have a pointer.

       Yes, we can state matrix using a function instead of a pointer or the like if-else:

We use state of the matrix successfully circumvented if-else ...

We can see, still pointer with.

02、

A pointer is stored - computer work necessary conditions for the implementation of the model!

       We look at the store - how a computer model of execution:

  given address, CPU can fetch data of the address.

  given address, CPU can write to the address value.

what does this mean?

       Want to work as long as the CPU, it is necessary to expose the entire memory address space for CPU, otherwise the CPU is a pile of useless gate, in other words,  all from memory! Operating memory is bound to use the pointer!

03、

In fact, C language is a simplified version of the assembly language. Finally, C language compilation Relay pointer created the world.

       Anyway, C language is a computer-oriented programming language , rather than business-oriented programming language, it maps the way computers work and not very good at describing the business logic, therefore, C language by hackers, computer programming such craftsmen lovers of their own favorite, but do not be business programmers to be seen as playing with the pointer does too cumbersome and complex, and believe it will go wrong.

       Storage - performing problem is that the model, to design complex band mechanism to prevent any access to memory, the resulting complex is a segment, paging, access control, and so the MMU mechanism, of course, depend on those mechanisms and CPU access pointer memory work does not conflict.

       The C language pointer used most definitely should be the embedded Linux kernel linked list of struct list_head of:

       It can be everything, it is a perfect interpretation of the pointer by C OOD, list_head is the base class for the world!

       By container_of macro, list_head can be converted to any object:

       The dependence of the conversion behind, it is a pointer:

       However, C language programming remains unfriendly to business, said earlier, it is the C language mapping computer work itself, if you want to make good use of C language, it is necessary to understand the computer theory , this is not a business programmer dishes, operational procedures members only write business logic, does not care how the computer works.

       These services can not say no programming language pointers, but they hide it pointer, they are exposed to the programmer a more business-friendly programming interface and syntax, its own pointer in the underlying processing problems, nothing more. Pointer is an objective reality, as long as you are using the storage - execution model computer, the pointer is everything.

       For those who love programming, there are a group of small partners together to learn is very important! If you are interested or in need, then we have a zero-based introductory programming learning exchange clubs , as well as learning video files, are welcome beginners and advanced in little friends!

- END -

Published 520 original articles · won praise 132 · views 80000 +

Guess you like

Origin blog.csdn.net/HUYA69/article/details/105142346