Ex W6

Class-specific Overloads of operator new/operator delete

Take the class not_on_heap and write CUTE unit tests that demonstrate the inability to allocate objects of this class on the heap. Attempt the following:

struct not_on_heap {
    static void* operator new(std::size_t sz) {
    	throw std::bad_alloc{};
    static void* operator new[](std::size_t sz){
    	throw std::bad_alloc{};
    static void operator delete(void *ptr) noexcept  {
    	// do nothing, never called, but should come in pairs
    static void operator delete[](void *ptr) noexcept {
    	// do nothing, never called, but should come in pairs

BoundedBuffer with dynamic allocation


Take your BoundedBuffer.h file and change the implementation and interface.

Otherwise, your BoundedBuffer should behave similarly to the original one.

You will get a new set of test cases similar to the previous ones, that follow that new syntactical frame.

Here: https:files/DynamicBoundedBufferTests.zip

Detailed Description

A major disadvantage of the BoundedBuffer, developed for the first Testat, is requirement to specify the size at compile-time. In this exercise you will change that. You have learned about dynamic heap memory management in the lecture using new and delete. Apply what you have learned about dynamic heap memory management to improve your BoundedBuffer in a way that it does not require to specify the capacity as a template argument but rather a constructor argument. This does not mean that the buffer now should be resizable, the size is still fix, but it can be specified at run-time.


BoundedBuffer<S, 23> buffer{};


BoundedBuffer<S> buffer{23};

Data Structure

You will not be able to use an std::array for storing the elements anymore (since it requires knowing the number of elements at compile-time). Instead, allocate an array of T objects on the heap using new T[]. And store the pointer to that array in your BoundedBuffer.

Element Access

Element access using at does not work using the plain array on the heap anymore. Therefore, you need to access the elements with the array index operator [].

T copy = values[0];  //reading the first element
values[23] = copy;   //writing the 24th element

Special Member Functions

The most interesting part is the implementation of the constructors and assignment operators. Think about how every special member function should behave:

Remark: You will recognize, if you have implemented the move operations properly, that you now need less move operations. Because only the pointers to the heap allocated array need to be moved the elements are left untouched.

Question: What would be the impact (for you as an implementer and a user of the BoundedBuffer) if you provided an std::initializer_list<T> constructor that derives the capacity from the number of elements in the list? I.e. the BoundedBuffer is created and filled completely with the elements in the list.

Test Cases (From Week 5): https:files/DynamicBoundedBufferTests.zip

Advanced: Make your BoundedBuffer work with non-default-constructible classes

For example, the following class would not be default-constructible.

struct X{
  X(int i) : x{i}{}
  int x;
The solution will require you to allocate "raw" storage by using operator new[] for a reasonably sized character array and placement new into that array. Please make sure, that alignment and sizing of the array follow the rules for the template argument type.

Here are example test cases for non-default-constructible elements: https:files/NonDefaultConstructibleTypeTests.zip

Last edited April 6, 2018