fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool AllocArray(int size, int **ptr)
  5. {
  6. *ptr = new int[size];
  7. if (ptr != NULL)
  8. return true;
  9. else
  10. return false;
  11. }
  12.  
  13. bool AllocArrayBad(int size, int *ptr)
  14. {
  15. ptr = new int[size];
  16. if (ptr != NULL)
  17. return true;
  18. else
  19. return false;
  20. }
  21.  
  22. int main()
  23. {
  24. int *ptr = NULL;
  25. AllocArray(10, &ptr);
  26. // 這裡印出來的不是0
  27. // 代表成功儲存了配置空間的位置
  28. cout << ptr << endl;
  29.  
  30. int *ptr2 = NULL;
  31. AllocArrayBad(10, ptr2);
  32. // 這裡印出來的是0
  33. // 代表並沒有儲存配置空間的位置
  34. cout << ptr2 << endl;
  35. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
0x9d9c008
0