Confused to while my overloaded operator won't pass values from new object

Thank you for taking your time and reading this, it's my first time posting so please if I asked incorrectly, let me know so I can correct my mistakes.

My question is this:
I'm using a binary operator overload(on +), so I can create a new object from two other objects. When I do compile, I do get a : warning C4172: returning address of local variable or temporary. My problem is that, while I see my newly created object "result" will take the proper data members, at the end of the function I wrote, I understand it goes out of scope and deletes itself, what I don't get is it won't return the values to the newly created object, that object just remains blank!

I'm very new in c++, coming from java, so I'm in the process of learning (And to be honest, this is a homework project, I'm not expecting a solution or anything, something simple as being pointed to the right direction will help!).

My header file
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
#ifndef LARGEINT_H
#define LARGEINT_H
#include <iostream>
#include <string> 
using namespace std;

class LargeInt
{
	friend istream& operator>> (istream& f, LargeInt &a);
	friend ostream& operator<< (ostream& f, const LargeInt &a);


	protected:


	public:
		LargeInt();
		~LargeInt();
		LargeInt(const LargeInt &a);
		int maxSize;
		int length;
		int * p;
		string bigInt;

		LargeInt& operator+ (const LargeInt& num2);
		LargeInt& operator= (LargeInt& num2);
};


#endif 


My cpp file
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
#include "LargeInt.h"

LargeInt::LargeInt()
{
	maxSize = 100;
	length = 0;
	p = new int[];
	bigInt = "NULL";
}
LargeInt::~LargeInt()
{
	// delete [] p;
}
LargeInt::LargeInt(const LargeInt &a)
{
	maxSize = a.maxSize;
	length = a.length;
	p = new int;
	for (int i = 0; i < a.length; i++)
		p[i] = a.p[i];
	bigInt = a.bigInt;
}

istream& operator>>(istream& f, LargeInt &a)
{
	f >> a.bigInt;
	a.length = a.bigInt.length();
	for (int i = 0; i < a.length; i++)
		a.p[i] = a.bigInt[i] - 48;
	return f;
	
}
ostream& operator<<(ostream& f, LargeInt &a)
{
	for (int i = 0; i < a.length; i++)
		 f << a.p[i];
	return f;
}

LargeInt& LargeInt::operator+(const LargeInt& num2)
{
	LargeInt result;
	int carry = 0;
	int i = length;
	int j = num2.length;
	for (i, j; i >= 0 || j >= 0; i--, j--)
	{
		p[i] = p[i] + num2.p[j] + carry;
		result.bigInt[i] = result.p[i];
	/*	if (carry == 1)
			carry--;
		else if (this->p[i] > 10)
		{
			result.p[i] = p[i] / 10;
			carry++;
		}*/
	}

	return result;
}

LargeInt& LargeInt::operator= (LargeInt& num2)
{
	LargeInt temp(num2);
	return *this;
}

int main()
{
	LargeInt large1, large2, large3;
	cout << "Enter your number.\n";
	cin >> large1;
	cout << "Enter your number.\n";
	cin >> large2;
	cout << "\n";
	large3 = large1 + large2;
	cout << "---- FIRST # ";
	cout << large1;
	cout << "\n---- SECOND # ";
	cout << large2;
	cout << "\n---- THIRD # ";
	cout << large3;
	cout << "\n---- THIRD LENGTH ";
	cout << large3.length;
	cout << "\n";
	
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
int & foo()
{
  int result = 42;
  return result;
  // The lifetime of the stack-allocated local variable ends here
}
// By the time the caller receives a reference, that reference
// would refer to deallocated memory.  Implementations are free
// to fail in any way they please. 


Return by value:
1
2
3
4
5
int foo()
{
  int result = 42;
  return result;
}

This invokes copy.
Last edited on
Hi thanks for taking your time with that, shortly before you replied I tried that and yet in my + operator, I tried to do an "object3 = object1 + object2" yet, when it does return the values, it stores in object1, and 3 comes out default. I believe this might be an error in my assigntment operator/copy constructor.. Sorry for asking all these questions.
LargeInt& LargeInt::operator+(const LargeInt& num2)

There are 3 objects in play here:

1) 'this' (ie, the left side of the +)
2) 'num2' (the right side)
3) 'result' (the sum that you want to return

note on this line:

 
		p[i] = p[i] + num2.p[j] + carry;


You are modifying p[i]... this is part of 'this'. This is wrong. You want to modify result.p[i] and only result.p[i]. Any changes you make to 'this' will stick.


In fact.. you should explicitly forbid all changes to 'this' by making the function const. Also... combine that with what keskiverto said about returning by value instead of by reference:

1
2
// change your function to this:
LargeInt operator+ (const LargeInt& num2) const;


Note the 2 changes here:

1) returning LargeInt instead of LargeInt&. This means you will return a copy of result, rather than a reference.

2) The const added to the end of it. This will make this const, which will disallow you to change it inside the function (that's a good thing).
Thanks guys, your suggestions worked, it is adding correctly now, but when I remove the //comments from my destructor, it seems as visual studio triggers a breakpoint in which just crashes my program.. yet if i leave the comments, the addition operator will add up to 5 individual digits, any more and it crashes.

Pointers are tricky stuff, man..
How many elements are in your array p?
p = new int[];
Your operator= doesn't really do anything.
Rework it out.
Hi guys,
thanks for the comments, @keskiverto, the elements in array p are obtained via a string length ((This program is meant to get a big number, and split it into an int array dynamically))

To be honest, when I posted this, it was around 4-5am and I pulled an all nighter so looking back at the code, it was pretty bad! I did optimize it, and fix it up, but more problems arise and I don't understand why it's going on. They're really weird, and it being Friday, I can't contact my professor for help, so I would highly appreciate it from you guys just to kick me to the right direction.

Problem 1) My destructor, when I leave it to delete [] p;, it triggers a breakpoint, which then eventually just crashes my program without even doing any addition. Yet if i //delete []p, it will do the addition, but then eventually just crash the program.

Problem 2) my operator+, it now will add arrays fully, which is good! It didn't before, but it returns values in a weird way.
Example:
Large1 would be 111111111
Large2 would be 222222222
so then Large3 would be 333333333
but instead, I get Large1 & Large3 being 333333333

Problem 3) operator+ again, when i create a new object, result, and try to do all the functions I need on it, the program will crash. Yet if I invoke the copy constructor by result(*this), it will return the values, but in the way I mentioned in problem 2.

That seems to be it, I've went over all my functions, looking to see if I did any small errors, and I can't find any at all!

new header:
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
#ifndef LARGEINT_H
#define LARGEINT_H
#include <iostream>
#include <string> 
using namespace std;

class LargeInt
{
	friend istream& operator>> (istream& f, LargeInt &a);
	friend ostream& operator<< (ostream& f, const LargeInt &a);

	public:
		LargeInt();
		~LargeInt();
		LargeInt(const LargeInt &a);
		int maxSize;
		int length;
		int * p;
		string bigInt;

		LargeInt operator+ (const LargeInt num2) const;
		LargeInt& operator= (const LargeInt& num2);
};


#endif 


new cpp file:
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
#include "LargeInt.h"

LargeInt::LargeInt()
{
	maxSize = 100;
	length = 0;
	p = new int[];
	bigInt = "NULL";
}
LargeInt::~LargeInt()
{
	cout << "DESTRUCTOR CALLED" << endl;
	delete [] p;
}
LargeInt::LargeInt(const LargeInt &a)
{
	maxSize = a.maxSize;
	length = a.length;
	p = a.p;
	bigInt = a.bigInt;
}

istream& operator>>(istream& f, LargeInt &a)
{
	f >> a.bigInt;
	a.length = a.bigInt.length();
	for (int i = 0; i < a.length; i++)
		a.p[i] = a.bigInt[i] - 48;
	return f;
	
}
ostream& operator<<(ostream& f, LargeInt &a)
{
	for (int i = 0; i < a.length; i++)
		 f << a.p[i];
	return f;
}

LargeInt LargeInt::operator+(const LargeInt num2) const
{
	LargeInt result(*this); // If I don't "copy" *this, it won't return values to the new object, don't understand why..
	int carry = 0;
	int i = length;
	int j = num2.length;
	for (i, j; i >= 0 || j >= 0; i--, j--)
	{
		result.p[i] = p[i] + num2.p[j] + carry;
		result.bigInt[i] = result.p[i];
	/*	if (carry == 1)
			carry--;
		else if (this->p[i] > 10)
		{
			result.p[i] = p[i] / 10;
			carry++;
		}*/
	}
	return result;
}

LargeInt& LargeInt::operator= (const LargeInt& num2)
{
	if (this != &num2)
	{
		maxSize = num2.maxSize;
		length = num2.length;
		p = num2.p;
		bigInt = num2.bigInt;
	}
	return *this;
}

int main()
{
	#if defined(DEBUG) | defined(_DEBUG)
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
	#endif 
	LargeInt large1, large2, large3;
	cout << "Enter your number.\n";
	cin >> large1;
	cout << "Enter your number.\n";
	cin >> large2;
	cout << "\n";
	large3 = large1 + large2;
	cout << "---- FIRST # ";
	cout << large1;
	cout << "\n---- SECOND # ";
	cout << large2;
	cout << "\n---- THIRD # ";
	cout << large3;
	cout << "\n---- THIRD LENGTH ";
	cout << large3.length;
	cout << "\n";
	
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
LargeInt::LargeInt()
{
  p = new int[]; // What is the size of the block here? 0?
  // Allocating nothing is not ok
}

LargeInt::LargeInt(const LargeInt &a)
{
  p = a.p; // Both 'a' and 'this' do now "own" the same memory block
  // both would deallocate same memory in their destructors.
  // Definitely not ok
}
@keskiverto

Goodness man! You did exactly what I wanted, you kicked me in the total correct direction! The allocating nothing was a very rookie move on my half, an oversight. But the pointer's not owning the same memory block, that right there opened my eyes! My program is 100% functional, thank you so much!
Topic archived. No new replies allowed.