From the course: Complete Guide to C++ Programming Foundations

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Using objects with pointers

Using objects with pointers

- [Lecturer] The previous example may have shown you how pointers work, but pointing to existing variables in memory isn't particularly useful. One of the most important applications of pointers is dynamic memory management. When you declare a regular variable, you are statically allocating memory for it. The compiler already knows that space in memory will be needed. However, most real applications create variables and objects in memory on the fly during runtime. This is known as dynamic memory management, and it happens in the heap. In C, it is common to dynamically allocate memory using the malloc family of functions, which is short for memory allocation. You as a programmer are responsible of freeing up the memory you allocated in runtime to avoid memory leakage, but this is where C++ plus really shines. The use of pointers is supported by the language to dynamically allocate memory for new objects when calling the class constructor and also to free their memory when you no longer…

Contents