14

Recently covered pointers in class. I know how to use them/Have an understanding of what they do, but why would I need to use a pointer in the first place? What problem would need to be solved through the use of pointers?

Comments
  • 8
    Linked lists, other complex data structures. Existing interfaces to libraries that require pointers. Interfacing directly to hardware. Passing objects to functions. I was just reading about how pointers are often preferred for this in game engines as there can be performance benefits. Dynamically creating objects and pointing to them. I am sure the list can go on and on. I am guessing there are numerous uses in high performance computing.

    Arguably many of these could be done with references. Also, if possible smart pointers should be used for RAII. Although for passing pointers this can be tricky.
  • 9
    In C, you use pointers because handing around a pointer doesn't involve much copying - but that would be different for handing around a large struct through several layers and functions.

    Another typical use is memory mapped IO where a specific memory address doesn't actualy contain memory, but peripherals like serial ports, configuration registers and so on.

    In C++, direct C style pointers are not considered best practice anymore. You'll instead use appropriate STL container types and references wherever you can.
  • 0
  • 1
    Collections like lists, trees, etc. Circular linking of types (most languages handle this through implicit references, but that's what's going on under the hood). Performance considerations (although this is dangerous and you can make things worse).

    Collections are the single biggest reason.
  • 4
    Imagine managing a library full of books... Someone comes and wants "Animal Farm". Will you go, fetch the book, photocopy it, and give them the copy, or direct them to the shelf where the original copy is stored?
  • 3
    Like @Fast-Nop said, why carry a kindle when you can lug around the entire library?

    But for a more relevant example: linked lists, etc. Individual nodes can store a pointer to other related nodes, like a family tree or links in a chain.
  • 4
    Dynamically sized allocation of memory, type erasure (dynamic dispatch of function calls), avoiding expensive copying. Every language that you don't have first-class pointer types in is probably using pointers for everything under the hood (Java, c#, etc)
  • 0
    Like others said. Instead of passing/storing copies of big arrays, you just point to them instead. Saves space because you dont have to store the same array multiple times and saves time spent copying it.

    And the other reasons.
  • 0
    Function pointers!. Every function has a pointer which improves the speed of function calling
  • 3
    @paku Function calls via function pointers are slower than direct calls because they have an additional level of indirection. You will only use that if the function you call is determined at runtime, not at compile time.

    Very useful for dynamic callbacks depending on the program state. Or if you want to do some level of object orientation in C.
  • 2
    @paku Completely backwards.
  • 0
    @Root there is nothing backward here. At the kernel level there are function pointers
  • 1
    @paku Yeah but not for improving speed, that's nonsense.
  • 2
    @paku "Take-out containers always have sealed lids, which makes it faster to open!"

    Backwards. A function pointer is always slower, not always faster.
  • 2
    @Root or even more generally: compile time (static) resolution is always faster than runtime (dynamic) resolution.

    Runtime resolution offers benefits but it's not speed.
Add Comment