1

I have written a program for a double-ended queue using an array in C++. One of the requirements of the assignment is to handle exceptions.

One of the exceptions we are supposed to check for is the out of memory exception.

Since it is a dynamically-sized queue I have a method that doubles the size of the array when it gets full. Like this:

try{
    doubleArray();
} catch (bad_alloc& e) {
    throw bad_alloc();
}

In the main file, this throws of bad_alloc is caught.

I have no idea how to test if this works or not. I'm using a Mac with X-code and also use the Mac terminal to compile and test the files. Does anyone know how to test this exception? i.e. set memory limits or something in either of those platforms? Or even if I'm doing it right or not?

EDIT (more info):

This is my implementation of doubleArray:

void Deque<T>:: doubleArray(){
int newSize = size_of_Deque * 2;
T *newElement = new T[newSize];

int temp = front;
int begin = (newSize/4);
front = begin;
int end = size_of_Deque + begin - 1;

while (begin<=end) {
    newElement[begin] = element[temp];
    begin++;
    temp = (temp + 1)% size_of_Deque;
}

element = newElement;
back = end;
size_of_Deque = newSize;
delete [] newElement;


}

So when the new array (with the double the size of the original array) is allocated that's when there could be the potential for out of memory case. That's why I did:

try{
    doubleArray();
}catch (bad_alloc& e) {
    throw bad_alloc();
}

Again, my main issue is that I have no idea how to test to see if it works. Is there any way to set memory limits in the terminal or in X-code?

eddie
  • 1,252
  • 3
  • 15
  • 20
GusGus
  • 230
  • 6
  • 16
  • What determines the size of the array? – Xiao Feb 23 '15 at 01:45
  • the array starts at size 8 and then if it gets full, it is doubled using the doubleArray() method. – GusGus Feb 23 '15 at 01:49
  • I mean, can you create your data structure with an arbitrary size? Does it have a method analogous to vector's [reserve](http://en.cppreference.com/w/cpp/container/vector/reserve)? – Xiao Feb 23 '15 at 01:50
  • If you are just going to catch and rethrow the exception without doing anything else, don't use a try block at all. – Neil Kirk Feb 23 '15 at 01:51
  • try { while (1) doubleArray(); } catch (bad_alloc& e) { cout<<"bad alloc!"<< endl; }. wait for your computer to get thrash and slow down. – thang Feb 23 '15 at 01:52
  • oooh that could work! @thang! – GusGus Feb 23 '15 at 01:59
  • Is 'doubleArray()' part of the data structure's public API? – Xiao Feb 23 '15 at 02:01

2 Answers2

0

You can just

throw std::bad_alloc{};

wherever you want to in your program, e.g. if the queue gets large or at random. For example something like this:

void doubleArray () {
    if (queueIsLarge) {
        throw std::bad_alloc("Thrown for test purposes.");
    }
    // Do the double array logic
}

would act like an artificial memory limit determined by queueIsLarge.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Yeah my question is how do I test to see if that works? I don't want to push items into the queue until i run out of memory, there has to be an efficient way to test if the exception works. – GusGus Feb 23 '15 at 01:46
  • @GusGus Well you throw where the `new[]` would throw and then you can see if your exception handling does the right thing. – Baum mit Augen Feb 23 '15 at 01:47
0

Create a test program that

  1. Creates an instance of your object
  2. Calls doubleArray() to increase the size to some number, say 65kb (how you will do this depends on the API of your data structure)
  3. Check that std::bad_alloc is thrown

To run the test program in a restricted memory environment

  1. create a user
  2. in a terminal, do ulimit -m 64. This limits the amount of memory available to the current user to 64 kb. The number depends on what you chose in step 2 above.
  3. run the program as that user
Xiao
  • 1,552
  • 2
  • 22
  • 20
  • how do you create a user in terminal? – GusGus Feb 23 '15 at 02:02
  • I don't own a Mac so I can't tell you exactly. This looks like it might be helpful: http://superuser.com/questions/202814/what-is-an-equivalent-of-the-adduser-command-on-mac-os-x. Of course, you could always just limit your current user's memory. Might require some fiddling of the numbers to get right though, as you would have to take into account how much memory is currently in use – Xiao Feb 23 '15 at 02:07