Advertisement
Guest User

Untitled

a guest
May 28th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main(){
  4. char *p = NULL; //empty pointer
  5. if (p) // evaluates to false as the pointer points to nothing
  6. std::cout <<"This won't get displayed!\n";
  7.  
  8. p = new char[50];//allocate some memory
  9.  
  10. if (p) {//memory has been allocated so this test will pass
  11. for (unsigned char i = 0; i != 50; i++) {
  12. char &v = *(p + i); // create a reference to
  13. v = 75; // set the character to K
  14. }
  15.  
  16. // print out those K's
  17. for (unsigned char i = 0; i != 50; i++)
  18. std::cout <<*(p + i);
  19.  
  20. // de-allocate the memory
  21. delete[] p;
  22.  
  23. std::cout <<"\n";
  24.  
  25. // apparently doesn't delete it all
  26. for(size_t i = 0; i != 50; i++)
  27. std::cout <<*(p + i);
  28.  
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement