Why hide pointers?

It was mention in a lecture that it is better to isolate a pointer or place it inside a data structure rather than use it directly. What are the benefits of this?
Never heard of this before. This is especially strange because you will usually pass around data structures as pointers. (IF we are talking about C here, that is).

Data structures, as the name implies, really are there for structuring data. Nothing else. (Again, if we are talking about C).
Manipulating pointers directly can be dangerous because you lack information on their usage and then you can expose your code to memory leaks, adress violation, ...
When you place it into a data structure it can hold others useful infos, the most common usage is smarts pointers with reference counting.
But it can have other usage. For example, imagine you want to send a message to an object. If you manipulate the pointer directly and you want to call a method on this object but the pointer is null you get a segfault. By using an intermediate structure you can provide a default object which will be used if there is no object. You can imagine plenty of other uses.
Isolate a pointer let you have more contol over the semantic of his utilisation.
Thanks aquaz that really helped my understanding of pointer usage.

Apologies for the somewhat ill-formed question.
Like RAII?
Yes, smart pointer rely heavily on RAII for reference counting
If you manipulate the pointer directly and you want to call a method on this object but the pointer is null you get a segfault. By using an intermediate structure you can provide a default object which will be used if there is no object.

please can any one explain this with the help of the example.
It's just an exemple of pointer encapsulation which popped into my mind, not really important.
But i can explain if you like with this class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
template <typename T>
class NullGuard
{
public:
	NullGuard(T* ptr = nullptr)
		: ptr_(ptr)
	{

	}

	void SetPtr(T* ptr)
	{
		ptr_ = ptr;
	}

	T* operator& ()
	{
		return ptr_ ? ptr_ : &default_;
	}

	T* operator-> ()
	{
		return operator&();
	}

	T& operator* ()
	{
		return *operator&();
	}
private:
	T* ptr_;
	static T default_;
};

template <typename T>
T NullGuard<T>::default_;


then you can call methods on your object and if it is null the default object is given.
Last edited on
Wouldn't you just check for nullptr? I don't see how providing a default object will do much good.
Do what you want, I repeat it is just an exemple i imagined to explain the author about pointer encapsulation.
Its about responsibility. A bare pointer has no responsibility. There can be several pointers pointing to the same bit of memory. Who is responsible for releasing that memory when it it no longer required? Well, its not the pointers, its the programmers. Nothing wrong with that, its C style programming.

When you encapsulate the pointer you hand over responsibility to the encapsulating object, so you don't have to think about it. It should be impossible to have memory leaks, segfaults etc however you use the class. If you have to think about what you're doing as a class user than the encapsulation isn't good enough.
Topic archived. No new replies allowed.