fueanta

Dynamic Allocation in Cpp [Syntax]

Oct 22nd, 2016
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "iostream"
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6.     int *x = NULL, *y = NULL;
  7.  
  8.     x = new int[5]; // int x[5]
  9.     y = new int(5); // *y = 5
  10.  
  11.     for (int i = 0; i < 5; i++) {
  12.         *(x + i) = i;
  13.     }
  14.  
  15.     for (int i = 0; i < 5; i++) {
  16.         cout << " " << x[i];
  17.     }
  18.  
  19.     cout << endl;
  20.  
  21.     delete []x;
  22.  
  23.     x = NULL;
  24.  
  25.     cout << *y << " is in " << y << endl;
  26.  
  27.     delete y;
  28.  
  29.     y = NULL;
  30.  
  31.     return 0;
  32. }
RAW Paste Data