1

I had been in an interview and asked to give an example or scenario in CPP where we can't proceed without pointers, means we have to use pointer necessarily. I have given an example of function returning array whose size is not known then we need to return pointer that is name of the array which is actually a pointer. But the interviewer said its internal to array give some other example.

So please help me with some other scenarios for the same.

Akash
  • 81
  • 5

5 Answers5

2

If you are using a C Library which has a function that returns a pointer, you have to use pointers then, not a reference.

Hayden
  • 2,170
  • 2
  • 11
  • 21
2

There are many other cases (explicitly dealing with memory, for instance) - but these two came to my mind first:

linked data-structures

How: You need to reference parts of your structure in multiple places. You use pointers for that, because containers (which also use pointers internally) do not cover all your data-structure needs. For example,

  class BinTree {
      BinTree *left, *right;
  public:
      // ...
  };

Why there is no alternative: there are no generic tree implementations in the standard (not counting the sorting ones).

pointer-to-implementation pattern (pimpl)

How: Your public .hpp file has the methods, but only refers to internal state via an opaque Whatever *; and your internal implementation actually knows what that means and can access its fields. See: Is the pImpl idiom really used in practice?

Why there is no alternative: if you provide your implementation in binary-only form, users of the header cannot access internals without decompiling/reverse engineering. It is a much stronger form of privacy.

Community
  • 1
  • 1
tucuxi
  • 15,614
  • 2
  • 36
  • 70
2

Anyplace you would want to use a reference, but have to allow for null values

This is common in libraries where if you pass a non zero pointer, it will be set to the value

It is also a convention to have arguments to a function that will be changed to use a pointer, rather than a reference to emphasize that the value can be changed to the user.

Glenn Teitelbaum
  • 9,339
  • 3
  • 31
  • 75
1

Here are some cases:

  • Objects with large lifetime. You created some object in function. You need this object afterwards (not even copy of it). But if you created it without pointers, on stack - after function would finish, this object would die. So you need to create this object using dynamic memory and return pointer to it.

  • Stack space is not enough. You need object which needs lot of memory, hence allocating it on the stack won't fit your needs, since stack has less space than heap usually. So you need to create the object again using dynamic memory on heap and return pointer to it.

  • You need reference semantics. You have structure which you passed to some function and you want the function to modify this structure, in this case you need to pass a pointer to this structure, otherwise you can't modify the original structure, since copy of it will be passed to the function if you don't use pointers.

Note: in the latter case, indeed using pointer is not necessary, since you can substitute it using reference.

PS. You can browse here for more scenarios, and decide in which cases are pointer usages necessary.

Community
  • 1
  • 1
gmoniava
  • 18,228
  • 5
  • 33
  • 74
  • You need reference semantics ... where the reference can be `null` – Glenn Teitelbaum Jul 05 '15 at 12:43
  • Containers such as vector, set or map store their items in heap (transparently for the end-user). Unless individual items are very large, you can avoid news in your code. – tucuxi Jul 05 '15 at 14:11
  • @tucuxi: So you are suggesting if I want to declare large array of structs say, which I know will not fit on stack, I declare a vector and add these elements instead? and they will be on heap and no issues? – gmoniava Jul 05 '15 at 16:20
  • @Giorgi if you need to keep around N structs of size S each, and N is large and S is small (say, <100KB), then you can avoid pointers in your code, and rely on the heap being used correctly within the containers. – tucuxi Jul 06 '15 at 09:36
  • Than you all for your replies.... That is a great help. Now I think, I am able to give some scenarios where we have to use the pointers – Akash Jul 14 '15 at 11:21
0

pointers are important for performance example of this are for functions. originally when you pass a value in a function it copies the value from the argument and stores to the parameter but in pointers you can indirectly access them and do what you want

Kryssel Tillada
  • 180
  • 2
  • 13