Advertisement
obernardovieira

Memory Allocation using std::size_t

Sep 12th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstddef>
  3.  
  4. struct pes {
  5.     int *chave;
  6. };
  7. pes pessoa[2];
  8.  
  9. int main() {
  10.     const std::size_t N = 10;
  11.     pessoa[0].chave = new int[N];
  12.     pessoa[1].chave = new int[N];
  13.  
  14.     std::cout << sizeof(pessoa[0].chave) << std::endl;
  15.     std::cout << sizeof(pessoa[1].chave) << std::endl;
  16.  
  17.     for (std::size_t n = 0; n < N; ++n)
  18.         pessoa[0].chave[n] = n;
  19.  
  20.     for (std::size_t n = 0; n < N; ++n)
  21.         pessoa[1].chave[n] = n+20;
  22.  
  23.     for (std::size_t n = 0; n < N+1; ++n)
  24.         std::cout << pessoa[0].chave[n] << std::endl;
  25.  
  26.     for (std::size_t n = 0; n < N; ++n)
  27.         std::cout << pessoa[1].chave[n] << std::endl;
  28.  
  29.     delete[] pessoa[0].chave;
  30.     delete[] pessoa[1].chave;
  31.    
  32.  
  33.     system("pause");
  34.     return 1;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement