user1325414
user1325414

Reputation: 87

What can pointers do that are otherwise impossible to implement?

So I'm having a hard time grasping the idea behind pointers and all that memory allocation.

I'm thinking nowadays with computer as powerful as they are right now why do we have to use pointers at all?

Isn't there always a workaround to do things without the help of pointers?

Upvotes: 1

Views: 192

Answers (3)

6502
6502

Reputation: 114579

I remember reading somewhere that Dijkstra was assessing a student for a programming course; this student was quite intelligent but s/he wasn't able to solve the problem because there was sort of a mental block.

All the code was sort of ok, but what was needed was simply to use the expression

a[a[i+1]] = j;

and even if being so close to the solution still the goal seemed to be miles away.

Languages "without pointers" already exist... e.g. BASIC. Without explicit pointers, that is. But the indirection idea... the idea that you can have data to mean just where to find other data is central to programming.

The very idea of array is about being able to use computed values to find other values.

Trying to hide this idea is an horrible plan. According to Dijkstra anyone that has been exposed to the BASIC language has already received such a mental mutilation that is impossible to recover as a good programmer (and probably the absence of explicit indirection was one of the problems).

I think he was exaggerating.

Just a bit.

Upvotes: 0

Jason Larke
Jason Larke

Reputation: 5609

Pointers are incredibly powerful. Just because computers have a faster processing time nowdays, doesn't mean that's any reason to abandon something as essential as pointers. Passing around giant chunks of memory on the stack is inefficient at best, catastrophic at worst. With pointers, you only need to maintain a reference to where the data resides, rather than duplicating huge chunks of memory each time you call a function.

Also, if you're copying all the data every time, how do you modify the original data? Aside from returning the copy of the structure in every call that touches it.

Upvotes: 1

Attila
Attila

Reputation: 28772

Pointers are an indirection: instead of working with the data itself, you are working with (something) that points to the data. Depending on the semantics of the language, this allows many things: cheaply switch to another instance of data (by setting the pointer to point to another instance), passing pointers allows access to the original data without having to make (a possibly expensive) copy, etc.

Memory allocation is related to pointers, but separate: you can have pointers without allocating memory. The reason you need pointers for memory allocation is that the actual address the allocated block of memory resides is not known at compile time, so you can only access it via a level of indirection (i.e. pointers) -- the compiler statically allocates space for the pointer that will point to the dynamically allocated memory.

Upvotes: 1

Related Questions