Do we need pointers?

Is there any case or concrete example where we need pointers to work? that is, something that requires pointers without using them? We could send pointers as a parameter to the function, but this could be done with the returned data. We can use pointers to change more than one variable in a function, but there is also a workaround for this.

Do we need pointers?

I know this is a stupid question, sorry, but in the circumstances it seems right to me. I just want to know if there is something that we cannot do with pointers.

+4
source share
5 answers

Pointers can be avoided when developing high-level language paradigms — for example, take Java, but they tend to reappear in real-life practical implementations of the functions that the language claims to provide.

Unless you are using some kind of dynamically reconfiguring FPGA architecture, a colony of state machines, or a similar idea like your computer, the actual computers are not very object oriented. We do not perform operations on the storage device, instead, we obtain data, perform calculations on it and write them back. To find data in memory, we use the concept of an address (there are things called content-addressable memories, but for most purposes they are not preferable). After you decide that your data (or the body of the function or structure containing both) is in memory at some address, you need to find out how you are going to process the address. You can write the address in another memory location, and we have the most familiar pointer concept. You can save the address on the stack, but it's just storage relative to the register called the "stack pointer". Or you can save the address in a register or instruction word.

But if you look at the actual format of the instruction word, for non-trivial processors, “registers” are actually numbered locations in a special internal memory called a register file. The RISC tradition of numbering them makes this especially obvious, but this is true even in assembly language circuits, where registers are called - MOV or any instruction will have several bits that determine the type and format of the instruction, and several bits that encode the register number for each operand. Therefore, registration of indirect addressing is technically one memory cell containing the address of another, that is, a pointer. Similarly, the address of a data element that is directly encoded in a command word is also an example of one memory cell (program memory containing the command word) relating to another, where the data is actually stored.

Conclusion: you can hide pointers from a high-level programmer, but so far your implementation is based on

(address data memory / register file) ↔ CPU - ↔ (address program memory)

the idea of ​​computing, you cannot avoid them at the implementation level.

+5
source

Are pointers needed in high-level languages? In the strict sense, there is no Turing machine. However, we don’t actually encode Turing machines — we encode high-level languages, and developers need certain constructions to ease the mental burden of writing code. Pointers make certain abstractions (like linked lists) much easier to express. Therefore, although there may be no answer in the strictest interpretation of the word “need”, in practice the answer is “yes”, they need us.

Please read the StackOverflow FAQ.

+1
source

I find it very useful that pointers can be used as (efficient) array iterators.

For example, (C99):

ptrdiff_t stride = 8; double* data = ...; size_t size = ...; const double* end = data + size * stride; for (double* restrict p = data; p < end; p += stride) *p = ...; 

You can see that pointer arithmetic is quite useful to express intention. It is also useful to use pointer + step omissions to represent subarrays.

Some languages ​​prohibit the use of pointers for data management (but allow references), although in these languages ​​I never liked how they allow you to write functions that act on arrays. In C, you write, for example.

 void foo(const double* data, ptrdiff_t stride, size_t size, double* out); 

In C # you have to write

 void Foo(double[] data, int stride, double[] output); 

but now output forced to be a full-blown array, not a subarray of anything else, or a simple stack variable in case one element comes out (C # provides pointers if you really want to, but let's pretend it's not).

+1
source

Pointer functionality is a superset of links. Everything you can do with links that you can do with pointers. A pointer is a more powerful tool than a link, the reason C uses pointers. It is a low level powerful language.

is there any case or concrete example where we need pointers to the task

Yes, for example. for pointer arithmetic.

 //copy string to a buffer char buffer[1024]; strcpy(buffer, "hello "); strcpy(buffer + 6, "world!"); 

something can be done that requires pointers, without using them.

Of course you want to pass a large object to a method. Instead of copying it, you can pass a pointer to it, but it's better to pass a link.

 void foo(Object const& o); // <- this one is prefered void foo(Object *o); 

Here is a set of rules on how to pass a function object.

we could send pointers as a function parameter, but could this be done with the returned data?

It can be done. The problem with returning the pointer is this: "What is the location of the store pointed to by the pointer?" It is a dangerous idea to return a local variable . (C ++ 11 solved this problem with move semantics )

we can use pointers to change more than one variable in a function, but there is a workaround for this

Sorry, but I do not understand this.

Do we need pointers?

Yes we do.

0
source

You need to use pointers in many situations, especially if you take into account memory consumption and the time of your program, for example, when a large array must be passed to a routine that does not modify it, a one-way method may be preferable. However, passing an array to a function will require that the entire array be moved to the local storage area of ​​the routine. This would be costly both in time and in space. Because of this, large arrays are often passed by reference.

0
source

Source: https://habr.com/ru/post/1411143/


All Articles