Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib> // for malloc and free
- #include <iostream>
- using namespace std;
- int main()
- {
- // Buffer allocated on stack only requires a variable declaration
- // It will be deallocated as soon program goes out of scope, i.e exited
- int bufferOnStack[20];
- // Buffer allocated on heap requires a function call
- // * here is called integer pointer
- // It must be freed before exiting program otherwise the program will become vulnerable to memory leak
- int *bufferOnHeap1 = (int *)malloc(20); // memory allocation of 20 space
- int *bufferOnHeap2 = new int[20]; // memory allocation of 20 space
- // function to free heap chunk
- free(bufferOnHeap1);
- delete bufferOnHeap2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement