Shahab
Shahab

Reputation: 11

What is the causing my buffer to overrun?

Somehow in one of or more of my loops are overrunning my buffer but I can't catch it. It's either that or somehow my capacity or size is at 0 when it shouldn't be but either way I can't find the issue and I need help with my school assignment so please if you can help do it quick because my assignment due is very close.

Here is the header modulate.

#define _SOMESTUFF_

#include <ostream>
#include <stdexcept>
#include <string>





namespace CS52 {
    class Something {
    public:



        virtual int size() const = 0;
        virtual int capacity() const = 0;
        virtual std::string type() const = 0;
        
        virtual ~Something() {}


    };




    class SomeStuff : public Something {
    private:
        int _size = 0;
        int _capacity = 0;
        int* _data = nullptr;

        
        
       /* int backup_size_ = 0;
        int backup_capacity_ = 0;
        int* _backup = nullptr;*/



    public:
        SomeStuff();
        SomeStuff(int size, int value);
        SomeStuff(const SomeStuff& other);
        SomeStuff(SomeStuff&& other) noexcept;
        SomeStuff& operator=(const SomeStuff& other);
        SomeStuff& operator=(SomeStuff&& other) noexcept;
       
       
        ~SomeStuff();




        int& at(int index) const;
        void clear();
        // void backup();
        int* data() const;
        bool empty() const;
        void push_back(int element);
        void pop_back();
        int& operator[](int index) const;
        SomeStuff operator+(const SomeStuff& rhs);

       
       
       
        
        


        friend std::ostream& operator<<(std::ostream& out, const SomeStuff& s);
        
        
        int size() const override;
        int capacity() const override;
        std::string type() const override;



        

            
            
            
       


    };


#endif

and then the cpp file.




#include "SomeStuff.h"



namespace CS52 {

    SomeStuff::SomeStuff() : _size(0), _capacity(0), _data(nullptr) {}

    SomeStuff::SomeStuff(int size, int value) : _size(size), _capacity(size), _data(new int[_capacity])
    {

        for (int i = 0; i < _size; i++) {
            _data[i] = value;
        }
    }
    SomeStuff::SomeStuff(const SomeStuff& other) : _size(other._size), _capacity(other._capacity), _data(new int[_capacity])
    {

        for (int i = 0; i < _size; ++i) {
            _data[i] = other._data[i];

        }

    }
    SomeStuff::SomeStuff(SomeStuff&& other) noexcept : _size(other._size), _capacity(other._capacity), _data(other._data) {
        other._size = 0;
        other._capacity = 0;
        other._data = nullptr;
    }
    SomeStuff& SomeStuff::operator=(const SomeStuff& other) {
        if (this == &other) {
            return *this;
        }



        delete[] _data;


        _size = other._size;
        _capacity = other._capacity;


        _data = new int[_capacity];


        for (int i = 0; i < _size; ++i) {
            _data[i] = other._data[i];
        }

        return *this;

    }

    SomeStuff& SomeStuff::operator=(SomeStuff&& other) noexcept {

        if (this != &other) {
            delete[] _data;

            _size = other._size;
            _capacity = other._capacity;
            _data = other._data;












            other._size = 0;
            other._capacity = 0;
            other._data = nullptr;
        }

        return *this;
    }
    SomeStuff::~SomeStuff()

    {
        delete[] _data;
        //        delete[] _backup;


    }


    int& SomeStuff::at(int index) const {
        if (index < 0 || index >= _size) {
            throw std::out_of_range("index out of bounds");
        }
        return _data[index];

    }

    void  SomeStuff::clear() {
        _size = 0;

    }

    /*void  SomeStuff::backup() {


        delete[] _backup;

        backup_size_ = _size;
        backup_capacity_ = _capacity;
        _backup = new int[backup_capacity_];
        for (int i = 0; i < backup_size_; i++) {
            _backup[i] = _data[i];
        }
    }*/

    int* SomeStuff::data() const {
        return _data;
    }

    bool  SomeStuff::empty() const {
        return _size == 0;

    }

    void SomeStuff::push_back(int element) {
        if (_size == _capacity) {
         
            int new_cap = (_capacity == 0) ? 1 : _capacity * 2;
            int* new_data = new int[new_cap];

         
            for (int i = 0; i < _size; ++i) {
                new_data[i] = _data[i];
            }

            delete[] _data;
            _data = new_data;
            _capacity = new_cap;
        }
        _data[_size] = element;
        ++_size;
    }


    void  SomeStuff::pop_back() {
        if (_size > 0) {
            --_size;
        }
    }

   




   




    int& SomeStuff::operator[](int index) const {
        if (index < 0 || index >= _size) {
            throw std::out_of_range("index out of bound");

        }
        return _data[index];

    }

   
   

   

    SomeStuff  SomeStuff::operator+(const SomeStuff& rhs) {



        SomeStuff result(_size + rhs._size, 0);


        for (int i = 0; i < _size; ++i) {
            result._data[i] = _data[i];


        }


        for (int i = 0; i < rhs._size; ++i) {
            result._data[_size + i] = rhs._data[i];

        }



        result._size = _size + rhs._size;

        result._capacity = _size + rhs._size;
        return result;
    }

    std::ostream& operator<<(std::ostream& out, const SomeStuff& s) {
        for (int i = 0; i < s._size; ++i) {
            out << s._data[i] << " ";

        }
        return out;
    }



    int SomeStuff::size() const {
        return _size;

    }
    int SomeStuff::capacity() const {
        return _capacity;

    }
    std::string SomeStuff::type() const {
        return "SomeStuff";
    }
}

Help me solve this mystery and write all your suggestions and ideas as well (doesn't necessary need to be about the issue anything that can help me to improve my code)

Upvotes: 1

Views: 11

Answers (0)

Related Questions