So here are some supposed facts I've read and know.
1) Objects made (IGNORING NEW/DYNAMIC MEMORY) inside functions cannot be return (they disappear after functions are done)
Example:
std::string* getTestString()
{
std::string s = "STRING MADE IN FUNCTION";
std::cout << "(INSIDE FUNCTION) DEREFERENCE (ADDRESS OF STRING): " << *(&s) << std::endl;
std::cout << "(INSIDE FUNCTION) RETURN (ADDRESS OF STRING): " << &s << std::endl;
return &s;
}
//main function
{
std::string* s = getTestString();
std::cout << "(IN MAIN) STRING POINTER: " << s << std::endl;
std::cout << "(IN MAIN) ADDRESS OF (STRING POINTER): " << &s << std::endl;
std::cout << "(IN MAIN) DEREFERENCE OF (STRING POINTER): " << *s << std::endl;
}
The result from console:
(INSIDE FUNCTION) DEREFERENCE (ADDRESS OF STRING): STRING MADE IN FUNCTION
(INSIDE FUNCTION) RETURN (ADDRESS OF STRING): 0x7fff85791040
(IN MAIN) STRING POINTER: 0x7fff85791040
(IN MAIN) ADDRESS OF (STRING POINTER): 0x7fff85791088
The program has unexpectedly finished.
TO SUMMARIZE:
1) String s made inside function
2) ADDRESS of String s returned
3) Since the function is DONE, String s and stuff is GONE
4) In main, the ADDRESS of String s is returned, but what it is (used to) pointing to is GONE
5) You try to dereference that address and since the stuff is GONE, it crashes
This makes sense.
2) This example:
class TestObject
{
public:
TestObject();
~TestObject();
int getTestVariable();
private:
int testVariable = 9999;
};
//IN MAIN.CPP
TestObject testTestObject1()
{
TestObject testObject;
std::cout<<"(INSIDE FUNCTION) ADDRESS OF TESTOBJECT: "<<&testObject<<std::endl;
std::cout<<"(INSIDE FUNCTION) TESTOBJECT.VARIABLE: "<<testObject.getTestVariable()<<std::endl;
return testObject;
}
TestObject* testTestObject2()
{
TestObject testObject;
std::cout<<"(INSIDE FUNCTION) RETURN (ADDRESS OF TESTOBJECT): "<<&testObject<<std::endl;
std::cout<<"(INSIDE FUNCTION) TESTOBJECT.VARIABLE: "<<testObject.getTestVariable()<<std::endl;
return &testObject;
}
{
TestObject tObject1 = testTestObject1();
std::cout << tObject1.getTestVariable() << std::endl;
std::cout << "(IN MAIN) ADDRESS OF (TESTOBJECT): " << &tObject1 << std::endl;
TestObject* tObject2 = testTestObject2();
std::cout << tObject2->getTestVariable() << std::endl;
std::cout << "(IN MAIN) TESTOBJECT POINTER: " << tObject2 << std::endl;
}
The result in console:
(INSIDE FUNCTION) ADDRESS OF TESTOBJECT: 0x7fff1aa91520
(INSIDE FUNCTION) TESTOBJECT.VARIABLE: 9999
9999
(IN MAIN) ADDRESS OF (TESTOBJECT): 0x7fff1aa91520
(INSIDE FUNCTION) RETURN (ADDRESS OF TESTOBJECT): 0x7fff1aa914e0
(INSIDE FUNCTION) TESTOBJECT.VARIABLE: 9999
9999
(IN MAIN) TESTOBJECT POINTER: 0x7fff1aa914e0
TO SUMMARIZE:
Regardless of what we just found out in example #1, this example works! WTF BS! What StackOverflow said is that #1 is correct and why you should use new/dyanmic memory to return pointers, but #2 works, why?
1) testTestObject1() makes a TestObject inside function
2) then returns by reference(??) it should be by value but if you look, the addresses are the same? why?
3) testTestObject2() makes a TestObject inside function
4) then returns the address as a pointer
5) but the variable and object still exists even though it should be gone because the function is GONE
IN SHORT:
Can someone explain to me why objects that were made in functions still exists even after the functions are finished?
Why should C++ programmers minimize use of 'new'?