Question:
C++ Why would returning an object cause it to change values?
2007-04-04 21:02:54 UTC
Below is a member function which performs the intersection operation on the set. Running the debugger, everything is fine until I hit the return statement. Once executed, the return statement changes my set values from say, {1, 3, 5} to {-342325235452, -3343435353, -5555342242} or something crazy like that. What would cause return to do that?

Set Set::operator^( Set right )
{ Set temp;
for(imaSet.clear(); imaSet.isMore(); imaSet.moveNext())
{ if (right.InSet(imaSet.getCurrent()) && !temp.InSet(imaSet.getCurrent()))
temp.imaSet.appendNode(imaSet.getCurrent());
}
return temp;
}
Five answers:
H Pham
2007-04-04 23:18:10 UTC
Without looking at the definition for class Set, it is hard to say. However, my guess is that you don't have a correct copy constructor. A copy constructor would have a signature like this:



Set::Set( const Set & s )

{ // create new set, then fill in the content with items from s

}



When you return a local variable, copy-constructor is invoked implicitly. If you don't implement copy-constructor, compiler define one for you; however, its behavior is usually not desirable.
csanon
2007-04-04 21:24:28 UTC
Maybe because, let's see. temp is a stack variable that comes into scope once the operator is called. You go through the function, in which case you hit return. Return returns a value (which then gets used appropriately). But then temp goes out of scope since it is returned, and the memory for temp is no longer valid.



You're staring at crud in an invalid memory location, that's what.
yuja
2007-04-04 21:26:20 UTC
The weird numbers represent the location of the values, not the actual values themselves. For instance, the 1 in the first set is located at -342325235452.



Remember that with C++ you can pass by reference (like in Java) or by value (like in C). In this case, you're passing by value because you're getting the address rather than the value. Try using &temp. You may also have to play around with pointers to get the results you need.



I know this is a bit vague, since I haven't used C++ in a while, but I hope I said enough to at least get you started in the right direction.
nusdunda
2007-04-04 21:39:48 UTC
You do mean, send something (as Set) to this operator^ and then this variable is change to itself back to its calling line.



See the code, do you mean like this.



#include



struct SET

{

int a;

int b;

};



SET *get( SET *a )

{

a->a++;

a->b++;



return a;

};



void main()

{

SET myset;



myset.a=10;

myset.b=5;



get( &myset );



printf("%d %d",myset.a, myset.b);

}



First I keep 10 and 5 in myset. The send myset to get function by address. It increases a and b. View the result, you'll get 11 and 6 instead. This example show you how to send by address (reference) in C++. Hope it can help you.



Astalavista :)
2007-04-04 22:22:56 UTC
temp variables scope is limited to that method only,thats why it gives junk values


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...