fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define SIZE (1)
  5. struct arry
  6. {
  7. int *n = NULL;
  8. int size;
  9. arry() { n = new int[SIZE]; size = SIZE; }
  10. ~arry() { delete [] n; }
  11. void incSize() {
  12. int *temp = new int[size + 1];
  13. for (int i = 0; i < size; ++i) {
  14. temp[i] = n[i];
  15. }
  16. delete [] n;
  17. n = temp;
  18. size++;
  19. }
  20. };
  21.  
  22. int main() {
  23.  
  24. int a[10];
  25. int b[99];
  26. int *c = new int[77];
  27.  
  28. cout << "sizeof a = " << sizeof a << endl;
  29. cout << "(sizeof a) / sizeof(int) = " << (sizeof a) / sizeof(int) << endl;
  30. cout << "sizeof b = " << sizeof b << endl;
  31. cout << "(sizeof b) / sizeof(int) = " << (sizeof b) / sizeof(int) << endl;
  32. cout << "sizeof c = " << sizeof c << endl;
  33. cout << "(sizeof c) / sizeof(int) = " << (sizeof c) / sizeof(int) << endl;
  34. cout << "sizeof *c = " << sizeof *c << endl;
  35. cout << "(sizeof *c) / sizeof(int) = " << (sizeof *c) / sizeof(int) << endl;
  36. cout << "動的に確保された配列のサイズはsizeofでは取得できないのだよ" << endl;
  37.  
  38. cout << endl << "可変配列のテスト" << endl;
  39.  
  40. arry dyn;
  41.  
  42. dyn.n[0] = 100;
  43.  
  44. cout << sizeof dyn.n << endl;
  45. cout << dyn.size << endl;
  46.  
  47. dyn.incSize();
  48.  
  49. cout << sizeof dyn.n << endl;
  50. cout << dyn.size << endl;
  51.  
  52. dyn.n[1] = 200;
  53.  
  54. cout << dyn.n[0] << endl;
  55. cout << dyn.n[1] << endl;
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
sizeof a = 40
(sizeof a) / sizeof(int) = 10
sizeof b = 396
(sizeof b) / sizeof(int) = 99
sizeof c = 4
(sizeof c) / sizeof(int) = 1
sizeof *c = 4
(sizeof *c) / sizeof(int) = 1
動的に確保された配列のサイズはsizeofでは取得できないのだよ

可変配列のテスト
4
1
4
2
100
200