Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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. -
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. -
pk7611705yCollections 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. -
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?
-
Root797675yLike @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. -
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)
-
Quirinus7525yLike 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. -
paku735yFunction pointers!. Every function has a pointer which improves the speed of function calling
-
@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. -
Root797675y@paku "Take-out containers always have sealed lids, which makes it faster to open!"
Backwards. A function pointer is always slower, not always faster.
Related Rants
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?
question
pointers
school
c++
help