-2

I have a simple question that I have been unable to find an answer to.

If you want to know how to make/use/manipulate pointers, there are 1001 resources. But I'm wondering if this statement describes the point of pointers (pun intended):

Pointers are a portable, stack-based variable type that make object references easy.

Is this a fair statement? Any elaborations?

safetyOtter
  • 1,430
  • 14
  • 26
user3308043
  • 797
  • 2
  • 9
  • 20
  • 1
    Related: [Barriers to understanding pointers](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) and a very subjective question – legends2k Mar 20 '14 at 09:50
  • You have tagged this question with both C# and C++. C# doesn't support pointers unless in `unsafe` context, and even then it's very limited. In C++ on the other hand pointers are widely used. This question is too broad, and largely opinion based. – aevitas Mar 20 '14 at 09:59
  • Let me add a 1002nd resource to your list: http://klmr.me/slides/modern-cpp — With regards to the three points your statement makes, the first two points are wrong. The third point is arguable (for a given definition of “easy” it may be correct). – Konrad Rudolph Mar 20 '14 at 10:43
  • What would "stack-based" mean in this context? You can most certainly have pointers in a language implementation that doesn't use a runtime stack and/or only has global data. The point of a pointer is that it lets you refer to the location of a thing rather than the thing itself. It's essentially an array index. – molbdnilo Mar 20 '14 at 10:53

2 Answers2

4

Pointer is very powerful C/C++ construct. You can directly access memory using pointer.

Now let me evaluate your statement as per my knowledge.

Pointers are a portable, stack-based variable type that make object references easy.

Pointers are a portable - May not be, it can be dependent on bitness of your system. If you are using 16 bit pointer, then addressable memory would be 16 bit, same for 32 and 64 bit.

Stack-based variable - Can be on heap or stack, also can keep address of stack (in case of variable) or heap memory

make object references easy - May not be, you may need to use difficult de-referencing, like using * or arrow operator (->), . You also need to null check it.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • You say “yes, but” – in reality the answer is “no”: pointers have got nothing to do with stack vs. heap. – Konrad Rudolph Mar 20 '14 at 10:46
  • @KonradRudolph Actually, what I get is, he is asking, if pointer itself is stack based variable, means, where pointer resides, it resides in stack, but it can keep address of both stack and heap address. – Pranit Kothari Mar 20 '14 at 10:48
  • But that’s wrong, too: a pointer doesn’t have to live on the stack, it can just as well live on the heap (ignoring for now the fact that the C++ standard doesn’t mention either stack or heap). – Konrad Rudolph Mar 20 '14 at 11:31
  • @KonradRudolph So, is it possible to store variable (pointer/or normal) on heap? Means if I declare `int *p` can p go to heap instead of stack? Please let me know if I am taking it wrong. – Pranit Kothari Mar 20 '14 at 11:50
  • Of course it can. In the simplest case, do `int** ppi = new int*;`. If that’s confusing, consider `struct X { int* pi; }; X* px = new X;`. In both cases, you have a pointer on the heap. – Konrad Rudolph Mar 20 '14 at 12:31
2

From Wikipedia:

a pointer is a programming language object whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address

Although Stack Pointers are a particular implementation utilising a pointer, the more general Pointer isn't anything to do with a stack.

Dutts
  • 5,781
  • 3
  • 39
  • 61