Skip to end of metadata
Go to start of metadata

C++ is not that different from C in terms of memory management. It still allows using malloc and free, after all, but we also have the new and delete operators, which, for starters, are more type-safe and work better with constructors. Like other languages, C++ has the heap and the stack to store data. All variables declared inside a function are stored in the stack, and go away when you return from the function, except if we make it a new instance of an object using the "new" operator. Then it will live on in the heap, where data that can be accessed anytime in the program is stored. Here is an example of allocating a block of memory of size n (an array), although I also have functions that store a simple int pointer and initialize an int, which are much faster.

Labels
  • No labels