Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <cstdlib>
  4.  
  5. // Dumbest array type possible with lots of copying and rubbish code
  6. // Note absence of generated copy constructor calls for the array or its items
  7. template <typename Type>
  8. struct Array
  9. {
  10. Array()
  11. {
  12. }
  13. Array(const Array& rhs)
  14. : size(rhs.size)
  15. {
  16. items = new Type[size];
  17. for (int i = 0; i < size; i++)
  18. items[i] = rhs.items[i];
  19. }
  20. Array& operator= (const Array& rhs)
  21. {
  22. size = rhs.size;
  23. items = new Type[size];
  24. for (int i = 0; i < size; i++)
  25. items[i] = rhs.items[i];
  26. return *this;
  27. }
  28. void resize(int size_)
  29. {
  30. size = size_;
  31. items = new Type[size];
  32. }
  33. Type* items = nullptr;
  34. int size = 0;
  35. };
  36.  
  37. struct Obj
  38. {
  39. Obj()
  40. {
  41. printf("construct\n");
  42. }
  43.  
  44. Obj(const Obj&)
  45. {
  46. printf("copy construct\n");
  47. }
  48.  
  49. ~Obj()
  50. {
  51. printf("destruct\n");
  52. }
  53. };
  54.  
  55. _declspec(noinline) Array<Obj> Apply(int v)
  56. {
  57. Array<Obj> r;
  58. r.resize(rand()%v);
  59. return r;
  60. }
  61.  
  62. int main()
  63. {
  64. Array<Obj> x = Apply(7);
  65. for (int i = 0; i < x.size; i++)
  66. printf("%x", &x.items[i]);
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement