My Understanding of Pointers

What are pointers?
They are references to memory addreses.

Why do we need pointers?
To avoid passing around and copying large objects when instead we can just tell the program the location of the object that it wants.

How do you declare a pointer?

MyClass* thing = new MyClass(); // to declare on the heap.

// or
MyClass thing;
MyClass* ptrThing = &thing; // I think this is a pointer to something on the stack.. Good because it goes away when the function goes out of scope.

I believe that the & can be used to create references as well as “get the address of something.” If I used & on a pointer it would give me the memory location of my pointer. If I use it on an object it gives me the address of the object.

When I de-reference a pointer with the * operator I can get whatever the pointer is pointing to.

Right now I’m confused on what smart pointers are good for and when I have to manage memory manually vs. when the destructor is automatically called…

In new code, pretty much never manage memory yourself. Use unique_ptr/make_unique or shared_ptr/make_shared

Whole talk is good but relevant part is 12m9s if the timestamped link doesn’t work.

Privacy & Terms