Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Segementation Fault with std::allocator being used. I've copy and pasted my code where the segmentation fault occurs, my debugger says the seg fault happens

C++ Segementation Fault with std::allocator being used. I've copy and pasted my code where the segmentation fault occurs, my debugger says the seg fault happens on the line that I have bolded but I still haven't been able to find the error. If you could find it, it would be amazing and I'll be sure to upvote you. Reminder: the line I've bolded is the line that the segmentation fault is caught, not neccessarily where the error actually is.

#ifndef ARRAY_LIST_HPP #define ARRAY_LIST_HPP

#include #include #include #include

template class List { public: List(); List(T item); List(const List &rhs); List(List &&rhs); ~List(); List& operator=(const List &rhs); List& operator=(List &&rhs); size_t size() const noexcept; size_t capacity() const noexcept; void insert(T item) noexcept; void erase(T item); // Throws underflow_error if empty, range_error if item doesn't exist const T& at(size_t i) const; // Both at() functions can throw T& at(size_t i); // out_of_range exception size_t search(T item) const; // Throws domain_error if item doesn't exist private: static std::allocator alloc;

T *_begin; T *_end; T *_capacity;

void grow() noexcept; // EXTRA void displace(size_t i); // EXTRA void collapse(size_t i) noexcept; // EXTRA };

//Static Initializtion template std::allocator List::alloc;

// LIST CLASS IMPLEMENTATION template List::List() : _begin(nullptr), _end(nullptr), _capacity(nullptr) { }

template List::List(T item) : _begin(alloc.allocate(1)), _end(_begin), _capacity(_begin + 1) { alloc.construct(_end++, item); }

template List::List(const List &rhs) : _begin(alloc.allocate(rhs.size())), _end(_begin), _capacity(_begin + rhs.size()) { for (size_t i = 0; i < rhs.size(); i++) alloc.construct(_end++, rhs._begin[i]); }

template List::List(List &&rhs) : _begin(rhs._begin), _end(rhs._end), _capacity(rhs._capacity) { rhs._begin = rhs._end = rhs._capacity = nullptr; }

template List::~List() { if (_begin) { while (_end != _begin) { alloc.destroy(--_end); } alloc.deallocate(_begin, capacity()); } _begin = _end = _capacity = nullptr; }

template List& List::operator=(const List &rhs) { if (this != &rhs) { while (_end != _begin) { alloc.destroy(--_end); } alloc.deallocate(_begin, capacity()); _begin = alloc.allocate(rhs.size()); _end = rhs._end; _capacity = rhs._capacity; for (size_t i = 0; i < size(); i++) _begin[i] = rhs._begin[i]; }

return *this; }

template List& List::operator=(List &&rhs) { if (this != &rhs) { alloc.destroy(--_end); _begin = rhs._begin; _end = rhs._end; _capacity = rhs._capacity; rhs._begin = rhs._end = rhs._capacity = nullptr; }

return *this; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

Working with other project stakeholders for support.

Answered: 1 week ago