In this lesson, you’ll learn more about the concept of pointers with a colorful analogy. Pointers are used in many languages to increase efficency and help reduce the amount of memory used.
00:07 Let’s say you just had built a brand new office building that’s perfect in every way. Except one little thing: the sign says Real Java, when it’s supposed to say Real Python. Of course, you’re a programmer and not some sort of sign-fixer, so you do some internet research and you find a company that says that they specialize in changing signs, but they said that in order to change the sign, they’ll need a building to fix and so they give you their business address.
00:41 Your first thought is to simply ship them a copy of your actual building. Then, they could just modify the sign and ship the building back to you, and you can replace the incorrect building with the new one.
00:54 But an office building is really, really heavy, so that’ll never work. Plus, the company said that they’re not willing to ship an entire building back because of shipping costs, or something. In programming terms, the office building uses many bytes of memory.
01:11 Passing a copy of it is very computationally expensive. Frustrated, you call the company and ask if instead of sending any actual buildings, you can send them the address of your building in the mail.
01:31 memory intensive, than the actual building at that address. They agree, and so you send them the address. In other words, you pass your building by reference. Instead of having to return anything, the company can follow your address and that will let them modify your building directly. In terms of programming, you just took a copy of your building’s address, or memory address, and stored it in the envelope, or a pointer.
This way, you didn’t have to send them a copy of your actual building. This is just like in the last video where we sent a reference to the
x variable, which the
change_variable function dereferenced and modified directly.
02:44 And that’s how pointers are used for efficiency. If used right, they can reduce the memory usage of your program, which is especially useful on microcontrollers, like the Arduino, which have very limited amounts of memory for programs.
Become a Member to join the conversation.