4

Is there any case or specific example where we would require pointers for the job? i.e. could something that requires pointers be done without using them? We could send pointers as a function parameter but that could be done with returned data. We can use pointers to change more than one variable in a function but there is a workaround for that too.

Do we actually need pointers?

I know it is a silly question, sorry,but under the circumstances seems correct to me I just want to know if there is something we cannot do using pointers.

Xeo
  • 129,499
  • 52
  • 291
  • 397
Sourabh Bose
  • 189
  • 2
  • 10
  • No, we don't. If we did, how would any programming language in use today work? (well, except for **one** language). – Marko Topolnik May 07 '12 at 15:56
  • This isn't an appropriate question for SO, use http://programmers.stackexchange.com/ – rooftop May 07 '12 at 15:57
  • 9
    I think that this is a perfectly valid question. – Puppy May 07 '12 at 16:01
  • @DeadMG It may be a perfectly valid topic for discussion, but it is clearly off topic on SO. – Marko Topolnik May 07 '12 at 16:01
  • You might find this helpful:http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/437f509f-3e1e-42c0-a895-c92b8b45b39e – Onorio Catenacci May 07 '12 at 16:01
  • 1
    Pointers are necessary for binary trees, and many other data structures, and I can't think of a way to emulate them with other tools (other than C++ references, and even then it'd be slow. – Mooing Duck May 07 '12 at 16:02
  • 6
    @DeadMG this is basically a "Let Me Google That For You" question. I type "Why Do We Need Pointers?" into Google and get 10 excellent hits right away. Don't know if it belongs here or not but it's easily answered with just a bit of research. – Onorio Catenacci May 07 '12 at 16:03
  • Turing machines doesn't use pointers. QED. – esej May 07 '12 at 16:03
  • 2
    @esej - I prefer to think of the tape head as a pointer :) – Flexo May 07 '12 at 16:04
  • @MooingDuck Ahnentafel lists or first class functions should work. – Pubby May 07 '12 at 16:04
  • @esej We don't code in Turing machines. We code in high level languages which sometimes need constructs like pointers to ease the workload for the developer. – Onorio Catenacci May 07 '12 at 16:04
  • 3
    If we're venturing into the turing tar pit, the lambda calculus doesn't have pointers or a tape head. – Logan Capaldo May 07 '12 at 16:05
  • thats the thing,is it just for ease of developing or a necessity for some code. – Sourabh Bose May 07 '12 at 16:06
  • @esej Turing machines might require more memory than you have available, and it will surely require more time to code than you have. – David Rodríguez - dribeas May 07 '12 at 16:09
  • 3
    A lot depends on definitions. For example, where do you draw the line between a "pointer" and "not a pointer"? Quite a few data structures require *something* that refers to some other data, but that doesn't have to be a "pointer" as (for example) C or C++ uses the term. – Jerry Coffin May 07 '12 at 16:10
  • @MarkoTopolnik: And what would be the *one* language without pointers that you are thinking of? – David Rodríguez - dribeas May 07 '12 at 16:10

5 Answers5

5

Pointers can be avoided in the design of a high-level language's paradigms - take java for example - but they tend to show up again in actual practical implementations of the functionality the language claims to provide.

Unless you are using some sort of dynamically reconfiguring FPGA architecture, colony of state machines or similar idea as your computer, actual computing machinery is not very object-oriented. We don't perform operations in the storage device, instead we fetch data, do computations on it, and write it 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). Once you decide that your data (or function body, or struct containing both) is in memory at some address, you have to figure out how you are going to handle the address. You can write the address in yet another memory location and we have the most familiar concept of a pointer. You can store the address on the stack, but that's just storage relative to a register called the "stack pointer". Or you can store the address in a register or instruction word.

But, if you look at the actual format of an instruction word, for non-trivial processors "registers" are actually numbered locations in special on-chip memory called the register file. The RISC tradition of numbering them makes this particular obvious, but it's true even in assembly language schemes where registers are named - a MOV or whatever instruction will have a few bits that determine the type and format of instruction, and a few bits encoding the register number for each operand. Therefore, register indirect addressing is technically one memory location holding the address of another, which is to say a pointer. Similarly, the address of a data item which is directly encoded in an instruction word is also an example of one memory location (program memory containing the instruction word) referring to another where the data is actually kept.

Conclusion: you can hide pointers from the high level programmer, but as long as your implementation is based on the

(addressable data memory/register file) <-> CPU -<-> (addressable program memory)

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

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
1

Do we need pointers in high-level languages? In a strict, Turing machine sense, no. However, in reality, we don't code in Turing machines--we code in high-level languages and developers need certain constructs to ease the mental workload of writing code. Pointers make certain abstractions (linked lists for example) a lot easier to express. So while in the strictest interpretation of the word "need" the answer may be no, in practice, the answer is yes, we do need them.

Please read the StackOverflow FAQ.

Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
1

I think very useful that pointers can be used as (efficient) iterators over arrays.

For instance (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 the intent. Being able to pass pointers + strides arounds to represent subarrays is also useful.

Some languages disallow the use of pointers to manipulate data (but allow references), though in these languages I have never been comfortable with the way they let you write functions which act on arrays. In C you write eg.

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

In C# you would write

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

but now output is forced to be an full blown array, not a subarray of something else, or a plain stack variable in the case of one element output (C# provides pointers if you really want, but let us pretend it does not).

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
0

Functionality of pointers is a superset of references. Everything you can do with references you can do with pointers. Pointer is a more powerful tool than a reference, that's the reason why C use pointers. It's a low level powerful language.

is there any case or specific example where we would require pointers for the job

Yes, e.g. for pointer arithmetic.

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

could something that requires pointers could be done without using them.

Sure, you want to pass a big object to a method. Instead of copying it you can pass a pointer to it, but it may be better to pass a reference.

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

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

we could send pointers as a function parameter but that could be done with returned data?

It can be done. The problem with returning a pointer is "what is a location of storage that pointer points to?". It's a dangerous idea to return 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 that too

Sorry, but I don't understand this one.

Do we actually need pointers?

Yes, we do.

Community
  • 1
  • 1
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108
0

You need to use pointers in many situation especially when you consider the memory consumption and the time of your program, for example, when a large array is to be passed to a subprogram that does not modify it, a one way method may be preferred. However, pass the array to the function would require that the entire array be moved to a local storage area of the subprogram. This would be costly in both time and space. Because of this large arrays are often passed by reference.