4

I'm trying to learn C++, and from my understanding if a variable goes out of scope then it is destroyed and its memory is reallocated. If I have a class and it's method creates a variable, shouldn't that variable be destroyed after the method call? For example:

class TestClass {
public:
struct Pair{
    std::string name;
    int value;
};
void addPair() {
    //should be deleted after push_back is called?
    Pair x = Pair{ std::string{ "Test Object " }, counter++ };
    pairs.push_back(x);
}
void printPairs() {
    for (int i = 0; i < pairs.size(); i++) {
        std::cout << "pair { " << pairs[i].name << " : " << pairs[i].value << " } " << std::endl;
    }
}
void removePair() {
    pairs.pop_back();
}
private:
    int counter;
    std::vector<Pair> pairs;
};

But when I tried addPair() then printPairs() then removePair() it works fine. Why doesn't it throw an error saying invalid access to memory location?

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Doe
  • 51
  • 2
  • Your code doesn't contain any access to invalid variables. Can you edit your question to explain exactly where you think the problem is? It would help to post your actual code instead of describing it. – M.M Oct 19 '15 at 22:54

2 Answers2

8

You said:

from my understanding if a variable goes out of scope then it is destroyed and its memory is reallocated.

That is correct. "reallocated" is not correct word I would use. I would phrase that as: The memory used by the object is available to be used by other objects.

And then you asked:

If I have a class and it's method creates a variable, shouldn't that variable be destroyed after the method call?

That is correct.

However, your situation is different.

When you use:

pairs.push_back(x);

a copy of x is placed in pairs. The copy continues to live in pairs after the function returns. Hence, printPairs() and removePair() work just fine.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I upvote this, but I think that clarifying the `x` is a class variable instance helps. It's then extrudable by Pairs. And it sits well as a C++ complexion. – David Pulse Oct 19 '15 at 22:07
  • @DavidPulse, I am guessing that English is not your first language. I don't quite follow what you are trying to say. – R Sahu Oct 19 '15 at 22:14
5

First, access to variables out of scope is undefined behavior. The program might throw an error but it might even work well. So there's no guarantee an error will be raised.

Second, std::vector::push_back makes a copy of its arguments. So nothing to worry about when passing local variables to it.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • Your answer is completely rhetorical. You define no point of reference for him to go by. You almost point out to your ownself but obviously miss that it's 50/50 on if your right (about the behavior). And it wasn't his question. He asked why Pairs still existed after being hopped around through his functions. Theyre all relative to one another. It's a class behavior. – David Pulse Oct 19 '15 at 21:46
  • @DavidPulse "You define no point of reference for him to go by." Ironically, it's one link more than your answer. "it's 50/50 on if your right" Elaborate on that. "Theyre all relative to one another." Are you talking about C++? – cadaniluk Oct 19 '15 at 21:51
  • Links are for when you're dry. *"The program migh throw an error but it might even work well. So there's no guarantee an error will be raised."* the class variables and methods are relative. You like trolling much? Never got into it. Miss so much I guess. – David Pulse Oct 19 '15 at 21:54
  • @DavidPulse " the class variables and methods are relative" Got to be Physics: http://physics.stackexchange.com/ BTW, C++ has *functions*. Methods are "virtual functions" in C++. "Links are for when you're dry" Obviously. :-) – cadaniluk Oct 19 '15 at 21:57
  • Classes are more greenhorn as *functions*, but are methodological reasoning places them at methods. Let's be pure hierarchical. When your base to create something is inferring a deep understanding that you wish others to guild and borrow from, you will of course change a few terms. This makes them Bjarne's. This method-calling is for his classes. Which to my knowledge were complexes introduced by him in C++. – David Pulse Oct 19 '15 at 22:05
  • @DavidPulse Great. Still didn't answer my questions, still didn't explain your "complaints." – cadaniluk Oct 19 '15 at 22:12
  • Actually I did. I believe you might be a smithe person who disregards certain pragma and ascertains or in the least recognizes very little the properties of English grammar. – David Pulse Oct 19 '15 at 22:15
  • 2
    @DavidPulse The given answer is more than adequate at explaining the OPs question. The second paragraph of the answer alone is enough. Why argue with an answer that is *correct*? – Luke Joshua Park Oct 19 '15 at 22:29