无效的分配运行时错误

时间:2013-11-27 05:30:08

标签: c++ arrays pointers vector operator-overloading

我必须使用int复制一个vector类并重载一堆运算符。每次我尝试使用+, - 或/运算符时,我都会收到一个运行时错误,表示无效的分配大小:4294967295字节。任何反馈我如何改进我的代码也是受欢迎的。

我的代码: myArray.h

#include<iostream>
using namespace std;

class myArray{
private:
    int *A;
    int lenght;
    int maxSize;
public:
    myArray(){lenght = 0; maxSize = 0; A = new int[maxSize];}
    myArray(int s){maxSize = s; lenght = 0; A = new int[maxSize];}
    myArray(const myArray &M);
    ~myArray(){delete[] A;}
    const int getMaxSize(){return maxSize;}
    const int getLenght(){return lenght;}
    const myArray& operator +(const myArray& A); 
    const myArray& operator -(const myArray A);
    const int operator *(const myArray& A);
    const myArray& operator /(const myArray A);
    const myArray& operator +(int A); 
    const myArray& operator -(int A);
    const int operator *(int A);
    const myArray operator /(int A);
    const myArray operator ++();
    const myArray operator ++(int);
    const myArray operator --();
    const myArray operator --(int);
    myArray operator -();
    int operator [](int ind) const;
    myArray& operator =(const myArray& rho);
    void push(int n);
    int pop();
    void insert(int n, int pos);
    int remove(int pos);
    void resize(int newSize);
};

myException.h

#include<iostream>
#include<exception>
#include<string>
using namespace std;

class myException: public exception
{
private:
    int code;
    string reason;
public:
    myException(){code = 0; reason = "Unknown";}
    myException(int c, string r){code = c; reason = r;}
    friend ostream& operator <<(ostream& outputStream, const myException A);
};  

ostream& operator <<(ostream& outputStream, const myException A)
{
outputStream << "Code: " << A.code << "  Reason: " << A.reason << endl;
return outputStream;
}

myArray.cpp

#ifndef MYARRAY_H
#define MYARRAY_H
#include "myArray.h"
#include "myException.h"
//Copy contructor
myArray::myArray(const myArray &M)
{
    maxSize = M.maxSize;
    lenght = M.lenght;
    A = new int[maxSize];
    for(int i = 0; i < M.lenght; i++)
        A[i] = M.A[i];
}    
//Adds the elements of the array with each other and returns the result
const myArray& myArray::operator +(const myArray& secondArray)
{
    try
    {
        if(lenght != secondArray.lenght)
            throw myException(10, "Different sizes!");

        myArray result(secondArray);
        for(int i = 0; i < lenght;i++)
            result.A[i] = A[i] + secondArray.A[i];
        return result;
    }
    catch(myException& e)
    {
        cout << e;
    }
} 
//Subtracts the elements of the array with each other and returns the result
const myArray& myArray::operator -(const myArray secondArray)
{
    try
    {
        if(lenght != secondArray.lenght)
            throw myException(10, "Different sizes!");

        myArray result(secondArray);
        for(int i = 0; i < lenght;i++)
            result.A[i] = this->A[i] - secondArray.A[i];
        return result;
    }
    catch(myException& e)
    {
        cout << e;
    }
}  
//Gets the dot product of 2 vectors
const int myArray::operator *(const myArray& secondArray)
{
    try
    {
        if(lenght != secondArray.lenght)
            throw myException(10, "Different sizes!");

        int result = 0;
        for(int i = 0; i < lenght;i++)
            result += this->A[i] * secondArray.A[i];
        return result;
    }
    catch(myException& e)
    {
        cout << e;
    }
}  
//Divides the elements of the array with each other and returns the result 
const myArray& myArray::operator /(const myArray secondArray)
{ 
    try
    {
        if(lenght != secondArray.lenght)
            throw myException(10, "Different sizes!");

        myArray result(secondArray);
        for(int i = 0; i < lenght;i++)
            result.A[i] = this->A[i] / secondArray.A[i];
        return result;
    }
    catch(myException& e)
    {
        cout << e;
    }
}    
//Adds the elements of the array with an int and returns the result
const myArray& myArray::operator +(int A)
{ 
    myArray result(*this);
    for(int i = 0; i < lenght;i++)
        result = this->A[i] + A;
    return result;
} 
//Subtracts the elements of the array with an int and returns the result
const myArray& myArray::operator -(int A)
{
    myArray result(*this);
    for(int i = 0; i < lenght;i++)
        result = this->A[i] - A;
    return result;
}
//Gets the dot product of a vector multiplied by an int
const int myArray::operator *(int A)
{
    int result = 0;
    for(int i = 0; i < lenght;i++)
        result += this->A[i] * A;
    return result;
} 
//Divides the elements of the array with an int and returns the result
const myArray myArray::operator /(int A)
{  
    myArray result(*this);
    for(int i = 0; i < lenght;i++)
        result = this->A[i] / A;
    return result;
}    

//increments every element in the array by 1(Pre-increment)
const myArray myArray::operator ++()
{
    for(int i = 0; i < lenght; i++)
        ++A[i];

    return *this;
}
//increments every element in the array by 1(Post-increment)
const myArray myArray::operator ++(int)
{
    myArray temp(maxSize);

    for(int i = 0; i < lenght; i++)
        temp.A[i] = A[i]++;

    return temp;
}
//decrements every element in the array by 1(Pre-decrement) 
const myArray myArray::operator --()
{
    for(int i = 0; i < lenght; i++)
         --A[i];

    return *this;
}
//decrements every element in the array by 1(Post-decrement)  
const myArray myArray::operator --(int)
{
    myArray temp(maxSize);
    for(int i = 0; i < lenght; i++)
        temp.A[i] = A[i]--;

    return temp;
} 
//Makes every element in the array negative
myArray myArray::operator -()
{
    for(int i = 0; i < lenght; i++)
        A[i] = -A[i];
    return *this;
}     
//returns the number in the array using []
int myArray::operator [](int ind) const
{
    try
    {
        if(ind > lenght)
            throw myException(60, "Array index out of bounds");

        return A[ind];
    }
    catch(myException& e)
    {
        cout << e;
    }
} 
//Assignment operator
myArray& myArray::operator=(const myArray& B)
{ 
    delete [] A;
    A = new int[B.maxSize];
    lenght = B.lenght;
    maxSize = B.maxSize;
    for(int i = 0; i < B.lenght; i++)
    {
        A[i] = B.A[i];
    }
    return (*this);
}

//pushes the value inserted to the next available spot in the array 
void myArray::push(int n)
{
    try
    {
        if(lenght == maxSize)
            throw myException(30, "Not enough space");

        if(lenght == 0)
        {
            A[0] = n;
            lenght++;
        }
        else
        {
            for(int i = 0; i < lenght; i++)
            {
                if(i+1 == lenght)
                {
                    A[i+1] = n;
                    lenght++;
                    break;
                }
            }
        }
    }
    catch(myException& e)
    {
        cout << e;
    }
}  
//Removes the last element in the array and returns it
int myArray::pop()
{
    try
    {
        if(lenght <= 0)
            throw myException(60, "Array index out of bounds");

        int temp = A[lenght - 1];
        A[lenght - 1] = NULL;
        lenght--;
        return temp;
    }
    catch(myException& e)
    {
        cout << e;
    }
}  
inserts an element at the specified position
void myArray::insert(int n, int pos)
{
    try
    {
        if(pos > lenght)
            throw myException(60, "Array index out of bounds");

        for(int i = 0; i <= lenght; i++)
        {
            if(i == pos)
            {
                A[i-1] = n;
            }
        }
    }
    catch(myException& e)
    {
        cout << e;
    }
}  
//removes an element at a specified position an returns the value.
int myArray::remove(int pos)
{
    try
    {
        if(pos < 0 || (pos > lenght -1))
            throw myException(50, "Invalid Position");

        int temp = A[pos];
        A[pos] = NULL;

        for(int i = pos; i < lenght; i++)
        {
            A[i] = A[i+1];
        }

        return temp;

    }
    catch(myException& e)
    {
        cout << e;
    }

}    
//Re sizes the entire array
void myArray::resize(int newSize)
{
    int *B;
    B = new int[newSize];
    maxSize = newSize;
    for(int i = 0; i < lenght; i++)
        B[i] = A[i];

    delete[] A;
    A = B;
}

#endif

这只是测试myArray类

上所有内容的虚拟主体

的main.cpp

#include "myArray.h"

int main()
{
    int num;
    myArray vector1;
    myArray vector2(5);
    myArray vector3;
    vector1.resize(5);
    //cout << "Max Size: " << vector1.getMaxSize() << endl;
    for(int i = 0; i < 4; i++)
    {
        cin >> num;
        vector1.push(num);
    }

    for(int i = 0; i < 4; i++)
    {
        cin >> num;
        vector2.push(num);
    }

    vector3 = vector1 + vector2;

    for(int i = 0; i < 4; i++)
        cout << vector3.pop() << endl;

}  

1 个答案:

答案 0 :(得分:0)

在默认构造函数中创建零大小的动态数组。 但是在push方法中,如果长度为零,则尝试将值设置为它。

在C ++标准中它说:

  

取消引用作为零请求返回的指针的效果   大小未定义。

您只能将其删除。修复您的push方法