简体   繁体   English

C++ 访问静态分配对象的成员

[英]C++ access statically allocated objects' members

So I am trying to understand the state of the memory when I run the below code.因此,当我运行以下代码时,我试图了解 memory 的 state。 From my understanding, the two sub-trees left and right that are initialized within the if-statement should be considered non-existent once the if-block ends.据我了解,一旦 if 块结束,在 if 语句中初始化的左右两个子树应该被认为不存在。 However, when I run this code, the output from within the if-block is the same as the output after the if-block.但是,当我运行此代码时,if 块内的 output 与 if 块后的 output 相同。 I thought this might be due to the system not actually removing what is allocated, instead simply updating a static memory pointer.我认为这可能是由于系统实际上并未删除分配的内容,而是简单地更新了 static memory 指针。 So I allocate a large array in hopes of this overwriting anything that could potentially still exist after the if-block.所以我分配了一个大数组,希望这会覆盖在 if 块之后可能仍然存在的任何内容。 However, this doesn't seem to have any effect on the output.但是,这似乎对 output 没有任何影响。 When I instead call the test function, on returning to main, the access to the sub-trees' val members outputs some random value.当我改为调用测试 function 时,返回 main 时,对子树的 val 成员的访问会输出一些随机值。 This is in line with what I would expect.这符合我的预期。

Could someone either explain what is happening.有人可以解释发生了什么。 Why does a block not seem to delete any locally allocated memory, while a function does appear to?为什么一个块似乎没有删除任何本地分配的 memory,而 function 确实出现了?

#include<iostream>

using namespace std;

class Tree {
    public:
    Tree(int init_val) : val{init_val} {}; 
    Tree() = default;
        Tree* left = nullptr;
        Tree* right = nullptr;
        int val;
};

void test(Tree* const tree) {
        Tree left(10);
        Tree right(20);
        tree->left = &left;
        tree->right = &right;
        cout << "inside function:" << endl;
        cout << "left = " << tree->left->val << endl;
        cout << "left = " << tree->right->val << endl;
}

int main(int argc, char const *argv[]) {
    Tree root;
    int input;
    cin >> input;
    if(input > 10) {
        Tree left(10);
        Tree right(20);
        root.left = &left;
        root.right = &right;
        cout << "inside if-statement:" << endl;
        cout << "left = " << root.left->val << endl;
        cout << "left = " << root.right->val << endl;
    }
    int arr[1000000];
    //test(&root);
    cout << "outside test and if-block:" << endl;
    cout << "left = " << root.left->val << endl;
    cout << "left = " << root.right->val << endl;
\
}

Suppose you own a plot of land and bury a body in it.假设您拥有一块 plot 的土地并在其中埋葬了一具尸体。 Later you sell the land to someone else.后来你把土地卖给别人。 Then you remember the body and freak out, so you go back and dig it up in the night.然后你记得尸体并吓坏了,所以你 go 回来并在晚上把它挖出来。 Luckily for you, it's still there.幸运的是,它仍然存在。

Yes, you were trespassing, and there was no guarantee you'd find it, but since the new owner hadn't done anything yet to their land, it was still intact.是的,你侵入了,不能保证你会找到它,但由于新主人还没有对他们的土地做任何事情,所以它仍然完好无损。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM