0

I'm having this problem where I keep getting the bad access warning. I see this typically spurs from exceeding the array bounds but in this case I don't see how that can be the problem when I have the condition subscript >= 0 && subscript < size.

I've played around with the program a little bit and I cant seem to find a fix. I'm going to paste in a few code blocks where I think the issue may be coming from and if you guys can take a look and maybe point me in the right direction that would be great!

Header file.

class ArrayClass{
friend ArrayClass operator +( const ArrayClass &, const ArrayClass & );
friend ArrayClass operator +( const ArrayClass &, const float );
friend ostream & operator << ( ostream &, const ArrayClass & );
friend istream & operator >> ( istream &, ArrayClass &);
private: 
    float *floatArray;
    int size;
public:
    ArrayClass(){ init(); }
    ArrayClass(int s = 0){
        init();
        size = s;
        floatArray = new float[size];
    }
    void init();
    ArrayClass( const ArrayClass& );
    void operator=( const ArrayClass& );
    ~ArrayClass();
    const int operator [] ( int ) const;
    // Various other methods. Output, getter/setter, etc. 
} 

Source file. (Possible causes)

const int ArrayClass::operator [] (int subscript) const{
if(subscript >= 0 && subscript < size)
    return floatArray[ subscript ];   

return 0;
}

ArrayClass operator +( const ArrayClass& arr1, const ArrayClass& arr2){
    ArrayClass temp_arr(arr1.size);
    for (int i = 0; i < arr1.size; i++){
        temp_arr.floatArray[i] = arr1[i] + arr2[i];
    }
    return temp_arr;
}

ostream& operator << ( ostream & out, const ArrayClass &arr ){
    for (int i = 0; i < arr.size; i++)
        out << arr.floatArray[i];

    return out;
}

ArrayClass:: ArrayClass(const ArrayClass& that){
    // Initialize state
    init();

    // Assigns input to self
    *this = that;
}

void ArrayClass::operator=(const ArrayClass& that){
    if (this != &that){
        // Copy static varibles
        floatArray = that.floatArray;
        size = that.size;

        // Deletes existing memory
        if (floatArray)delete [] floatArray;

        // Allocate memory for object
        if(size)
            floatArray = new float[size];
        else
            floatArray = NULL;

        // Copy dynamic memory
        for(int i = 0; i < size; ++i)
            floatArray[i] = that.floatArray[i];
    }
}

ArrayClass:: ~ArrayClass(){
    if (floatArray) delete [] floatArray;    //malloc: *** error for object 
}

And then in the main I just enter an array size, add them result = arr1 + arr2; and print cout<< result;

UPDATE: Ok so i took the comments into mind and redid a lot of this and put up some updated code, but now I'm getting a new malic error (pointer being freed was not allocated). I know this was marked as a duplicate but I think the problem has shifted to a slightly so if you can take another look that would be extremely helpful. Side note: If i comment out the destructor I get the correct results but id like to have it in there.

Dom Bavetta
  • 104
  • 2
  • 13
  • 2
    Learn the Rule of Three. – Kerrek SB Jul 23 '14 at 21:23
  • Also, learn how to use `std::vector` instead of `float*` and dynamic allocation. Doing so would more than likely solve most, if not all of the issues you're seeing here. – PaulMcKenzie Jul 23 '14 at 21:25
  • @PaulMcKenzie I have guidelines for this that restrict me to only arrays. Trust me id rather use a vector. And I didn't realize this was a duplicate, I'll check out rule of three thread now. – Dom Bavetta Jul 23 '14 at 21:28
  • 1
    @DBR in your `operator+` you never initialise the `temp_arr` are you sure this isn`t causing your error? – UnholySheep Jul 23 '14 at 21:30
  • @DBR - You are *not* using arrays. You are using `float*`. That is a pointer. A pointer is not an array. – PaulMcKenzie Jul 23 '14 at 21:33
  • 1
    @DBR on a second look your entire `operator+` looks weird. It has a return value of `ArrayClass` yet you return a `float`? (There are some other minor issues as well, such as you not calling `delete[] floatArray` in the destructor and I would use `size_t` instead of `int` for `operator[]` (removes the need for checking if `subscript >= 0`)) – UnholySheep Jul 23 '14 at 21:48
  • @PaulMcKenzie I did a little reworking based on your responses but the program is still coming up with an error. I updated the code so if you have time could you look over it again and maybe give some suggestions! – Dom Bavetta Jul 23 '14 at 23:10
  • 1
    @DBR - 1) There is no need to check for NULL when issuing a call to `delete`. 2) Your `operator=` is supposed to return `*this`. 3) Your operator= is not exception safe. What if `new` throws an error when attempting to create the new array? You've deleted the original array already, causing your object to be in an inconsistent state. 4) Your `operator=` right at the start creates a memory leak by overwriting the original floatArray. Bottom line -- it is not easy to properly code such classes. I have yet to see a beginner get it totally right without a lot of outside help from experts. – PaulMcKenzie Jul 23 '14 at 23:37
  • @DBR - Seriously, the way to get this right is not to experiment, but to actually view real code (whether in a book, here on SO, etc.) and see how it's done. To be honest, you won't get it right by experimenting and hoping to find the magic bullet. This is the advice I would give to any beginner that has been assigned to do something like this -- forget about trying to do this totally on your own first, and instead get a real code sample showing the steps needed and the gotchas to look out for. – PaulMcKenzie Jul 23 '14 at 23:42
  • @PaulMcKenzie I appreciate the help, this is a very confusing concept for me, its very nit-picky, and and I will take you advice. I'll continue to look into this, specifically at completed programs as you said :) – Dom Bavetta Jul 24 '14 at 16:28

0 Answers0