placement_new.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2004 Michael Stevens
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*
  8. * Test placement new and array placement new for uBLAS
  9. * See if base pointer is effected by array count cookie
  10. */
  11. #include <boost/numeric/ublas/storage.hpp>
  12. #include <iostream>
  13. #include <new>
  14. // User defined type to capture base pointer on construction
  15. class udt {
  16. public:
  17. udt () {
  18. base_pointer = this;
  19. }
  20. ~udt () {} // required for GCC prior to 3.4 to generate cookie
  21. static udt* base_pointer;
  22. };
  23. udt* udt::base_pointer;
  24. int main ()
  25. {
  26. udt a;
  27. udt* ap = &a;
  28. // Capture placement new offsets for a udt
  29. new (ap) udt;
  30. int new_offset = int (udt::base_pointer - ap);
  31. new (ap) udt [1];
  32. int array_new_offset = int (udt::base_pointer - ap);
  33. // Print offsets - we expect 0,0 or 0,sizeof(std::size_t)
  34. std::cout << new_offset <<','<< array_new_offset << std::endl;
  35. // Return status
  36. if (new_offset != 0)
  37. return -1; // Very bad if new has an offset
  38. #ifdef BOOST_UBLAS_USEFUL_ARRAY_PLACEMENT_NEW
  39. bool expect_array_offset = false;
  40. #else
  41. bool expect_array_offset = true;
  42. #endif
  43. // Check match between config and array
  44. if (!expect_array_offset && array_new_offset != 0) {
  45. return -2; // Bad config should not enable array new
  46. }
  47. if (expect_array_offset && array_new_offset == 0) {
  48. return -3; // Config could enable array new
  49. }
  50. return 0;
  51. }