std::bad_alloc

I'm working on a program and was suggested I work on it one part at a time. Currently I'm working on overloading operator+ however when I try to use + I get a

"terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
"
The current code is:

VectorN.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef VECTORN_H
#define VECTORN_H

#include <iostream>

using namespace std;

class VectorN
	{
	private:
		float *vArray;
		int vSize;
	public:
		VectorN();
		VectorN(float vector[], int newSize);
		~VectorN();
		
		int size() const;
		void clear();
		float& operator[](int i);
		float operator[](int i) const;
		VectorN& operator=(const VectorN& rightOp);
		VectorN operator+(const VectorN& rightOp) const;
		
	friend ostream& operator<<(ostream &leftOp, const VectorN& rightOp);
	};

#endif 


VectorN.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "VectorN.h"
#include <iostream>
#include <iomanip>
#include <fstream>

VectorN::VectorN()
	{
	vArray = new float[vSize];
	vSize = 0;
	}
	
VectorN::VectorN(float vector[], int newSize)	
	{
	vSize = newSize;
	vArray = new float[vSize];
	if (vSize > 0)
		{
		for (int i = 0; i < newSize; i++)
			{
			vArray[i] = vector[i];
			}
		}
	else if (vSize = 0)
		{
		vArray = '\0';
		}	 	 
	}	   
	
VectorN::~VectorN()
	{
	delete[] vArray;
	}
	
ostream &operator<<(ostream &leftOp, const VectorN& rightOp)
	{
	int i = 0;
	
	leftOp << "(";
	if(rightOp.vSize > 0)
		{
		for (i = 0; i < rightOp.vSize - 1; i++)
			{
			leftOp << rightOp.vArray[i] << ", ";
    		}
		leftOp << rightOp.vArray[i];
		}	 
	leftOp << ")";
	
	return leftOp;
	}
	
VectorN& VectorN::operator=(const VectorN& rightOp)
	{
	if( this != &rightOp)
		{
		delete[] vArray;
		vArray = new float[rightOp.vSize];
		for (int i = 0; i < rightOp.vSize; i++)
			{
			vArray[i] = rightOp.vArray[i];
			}
		vSize = rightOp.vSize;	  
		}
	return *this;
	}

VectorN VectorN::operator+(const VectorN& rightOp)const
	{
	VectorN result = *this;

	for(int i = 0; i < rightOp.vSize; i++)
        {
        result.vArray[i] += rightOp.vArray[i];
        }

	return result;
	}
		  
int VectorN::size() const
	{
	return vSize;
	}
	
void VectorN::clear()
	{
	vSize = 0;
	}
	 	 	 	  
float& VectorN::operator[](int i)
	{
    return vArray[i];
	}
	
float VectorN:: operator[](int i) const
	{
	return vArray[i];
	}


Driver program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <stdexcept>
#include "VectorN.h"

using std::cout;
using std::endl;
using std::out_of_range;

int main()
   {
   int test = 1;

   cout << "\nTest " << test++ << ": Default constructor and printing\n" << endl;

   const VectorN v1;

   cout << "v1: " << v1 << endl;
   
   cout << "\nTest " << test++ << ": Array constructor and printing\n" << endl;
   
   float ar2[] = {1.0, 2.0, 3.0};

   const VectorN v2(ar2, 3);

   cout << "v2: " << v2 << endl; 
   
   cout << "\nTest " << test++ << ": Clear and size\n" << endl;
   VectorN v3(ar2, 3);
  
   cout << "The size of v3: " << v3 << " is " << v3.size() << endl;
   
   v3.clear();

   cout << "After clearing, the size of v3: " << v3 << " is ";
   cout << v3.size() << endl;
   
	cout << "\nTest " << test++ << ": Subscripting\n" << endl;

   float ar3[] = {-1.0, -3.0, -5.0, 7.0};
   const VectorN v4(ar3, 4);

   cout << "v4: " << v4 << endl;
   cout << "v4[0]: " << v4[0] << "  v4[1]: " << v4[1] << endl;
   cout << "v4[2]: " << v4[2] << "  v4[3]: " << v4[3] << endl;
  
   VectorN v5(ar3, 4);
   v5[0] = 17;  v5[1] = 3;  v5[2] = 0;  v5[3] = -9;
   cout << "v5: " << v5 << endl;
   
    cout << "\nTest " << test++ << ": Copy constructor\n" << endl;
  
   const VectorN v6(ar2, 3);
   const VectorN v7 = v6;

   cout << "v6: " << v6 << " size: " << v6.size() << endl;
   cout << "v7: " << v7 << " size: " << v7.size() << endl;

   VectorN v8(ar3, 4);
   VectorN v9 = v8;

   cout << "v8: " << v8 << " size: " << v8.size() << endl;
   cout << "v9: " << v9 << " size: " << v9.size() << endl;

   v8[1] = 300.0;
   cout << "Changing..." << endl;
   cout << "v8: " << v8 << " size: " << v8.size() << endl;
   cout << "v9: " << v9 << " size: " << v9.size() << endl;
   
   cout << "\nTest " << test++ << ": Assignment operator\n" << endl;
   VectorN v10(ar3, 4);
   VectorN v11;

   cout << "v10: " << v10 << endl;
   cout << "v11: " << v11 << endl;

   v11 = v10;

   v10[0] = 0.0;

   cout << "v10: " << v10 << endl;
   cout << "v11: " << v11 << endl;

   cout << endl;

   // Chained assignment
   VectorN v12, v13;

   cout << "v11: " << v11 << endl;
   cout << "v12: " << v12 << endl;
   cout << "v13: " << v13 << endl;

   v13 = v12 = v11;

   cout << "v11: " << v11 << endl;
   cout << "v12: " << v12 << endl;
   cout << "v13: " << v13 << endl;

   cout << endl;

   // Assignment to self

   v13 = v13;

   VectorN v14(ar2, 3);

   cout << "v13: " << v13 << endl;
  
   cout << "\nTest " << test++ << ": Addition and subtraction\n" << endl;

   float ar4[] = {-2.0, 3.0, -1.0};
   float ar5[] = {0, 1, 2, -3};

   const VectorN v15(ar2, 3), v16(ar4, 3);
   const VectorN v17(ar5, 4);

   cout << "v13: " << v13 << endl;
   cout << "v15: " << v15 << endl;
   cout << "v16: " << v16 << endl;
   cout << "v17: " << v17 << endl;
   
   cout << endl;
   cout << "v15 + v16 is " << v15 + v16 << endl;
   cout << "v13 + v17 is " << v13 + v17 << endl;
   return 0;
   }  
In your default constructor:
1
2
vArray = new float[vSize];
vSize = 0;

vSize is not yet initialized when you try to allocate memory for vArray.
You need to switch the 2 lines:
1
2
vSize = 0;
vArray = new float[vSize];


Your VectorN::VectorN(float vector[], int newSize) is also incorrect because you allocate before checking the size, badly handle the size 0 case and do not handle at all negative sizes.
A better approach would be:
1
2
3
4
5
6
7
8
9
10
11
12
VectorN::VectorN(float vector[], int newSize)	
{
    vSize = newSize;
    if( vSize < 0 )
        vSize = 0;

    vArray = new float[vSize];
    for (int i = 0; i < vSize; i++)
    {
        vArray[i] = vector[i];
    }
}


You should also define a copy constructor VectorN::VectorN(const VectorN& other)
Topic archived. No new replies allowed.