Check for realloc failure and bad subscripts

This commit is contained in:
Jesse Jones 2012-12-08 21:34:26 -08:00 committed by Brian Anderson
parent cf1c3d2da0
commit 6bab226fc5
1 changed files with 9 additions and 2 deletions

View File

@ -14,6 +14,7 @@
#include <inttypes.h>
#include <stddef.h>
#include <new>
/**
* A simple, resizable array list. Note that this only works with POD types
@ -69,8 +70,12 @@ array_list<T>::append(T value) {
template<typename T> int32_t
array_list<T>::push(T value) {
if (_size == _capacity) {
_capacity = _capacity * 2;
_data = (T *) realloc(_data, _capacity * sizeof(T));
size_t new_capacity = _capacity * 2;
void* buffer = realloc(_data, new_capacity * sizeof(T));
if (buffer == NULL)
throw std::bad_alloc();
_data = (T *) buffer;
_capacity = new_capacity;
}
_data[_size ++] = value;
return _size - 1;
@ -115,11 +120,13 @@ array_list<T>::index_of(T value) const {
template<typename T> T &
array_list<T>::operator[](size_t index) {
assert(index < size());
return _data[index];
}
template<typename T> const T &
array_list<T>::operator[](size_t index) const {
assert(index < size());
return _data[index];
}