Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include <cstdlib> // for malloc and free
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. // Buffer allocated on stack only requires a variable declaration
  8. // It will be deallocated as soon program goes out of scope, i.e exited
  9. int bufferOnStack[20];
  10.  
  11. // Buffer allocated on heap requires a function call
  12. // * here is called integer pointer
  13. // It must be freed before exiting program otherwise the program will become vulnerable to memory leak
  14. int *bufferOnHeap1 = (int *)malloc(20); // memory allocation of 20 space
  15. int *bufferOnHeap2 = new int[20]; // memory allocation of 20 space
  16.  
  17. // function to free heap chunk
  18. free(bufferOnHeap1);
  19. delete bufferOnHeap2;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement