Advertisement
xlujiax

C12_Punteros_con_MemoriaDinamica

May 4th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int * f_stack(){
  6.     int a[5] = {};
  7.     return a;
  8. }
  9. int * f_heap(){
  10.     //int b[5] = {0};
  11.     //int * a = b;
  12.     int * a = new int[5];
  13.     return a;
  14. }
  15. int main(int argc, char** argv) {
  16.     //int * b_stack = f_stack();
  17.     int b_stack[5] = {0};
  18.     int * b_heap = f_heap();
  19.     int * c = b_heap;
  20.     b_heap[0] = 10;
  21.     cout << "AddressMem stack: " << b_stack << endl;
  22.     cout << "AddressMem heap: " << b_heap << ":" <<*(b_heap+3)<< endl;
  23.     delete [] b_heap;
  24.     cout << "AddressMem heap (delete): " << b_heap << ":" <<(int)(*b_heap)<< endl;
  25.     b_heap = new int[10];
  26.     cout << "AddressMem heap (new): " << b_heap << ":" <<*b_heap<< endl;
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement