Why does the object created without using “new” operator gets the same address

南笙酒味 提交于 2019-12-07 18:36:38

问题


Going through this question, one answer said the object created are destroyed outside their scope, To get this concept clearly I wrote the following code:

#include <iostream>

using namespace std;

struct Node{
    int val;
    Node *next;
    Node(int x) : val(x) , next(NULL){}
};

int main(){
    for(int i=0;i<10;i++){
        int someval = rand()%100;
        Node somenode = Node(someval);
        printf("Address for object %d is %p \n",i+1, &somenode);
    }
}

I got the following output:

Address for object 1 is 0x7ffc32ff26b0 
Address for object 2 is 0x7ffc32ff26b0 
Address for object 3 is 0x7ffc32ff26b0 
Address for object 4 is 0x7ffc32ff26b0 
Address for object 5 is 0x7ffc32ff26b0 
Address for object 6 is 0x7ffc32ff26b0 
Address for object 7 is 0x7ffc32ff26b0 
Address for object 8 is 0x7ffc32ff26b0 
Address for object 9 is 0x7ffc32ff26b0 
Address for object 10 is 0x7ffc32ff26b0 

I understand that each object is destroyed every time the loop iterates and a new object is created; but why do all of them have the same address. This problem occurred to me when I was creating a linked list and I did not use the new operator to create the object instead just used the same code, and the list always pointed to the same node and I ran into an infinite loop. Why is the same address allocated to each object?


回答1:


You keep creating the object on the top of the stack, and then removing it (the object goes out of scope at the end of each loop iteration, and is deconstructed before the next iteration). Thus, the address of the top of the stack is the same everytime you allocate space for the object.

If you remove the loop and allocate multiple Nodes in a row, you'll see different addresses for each as the stack grows.




回答2:


This has to do with local variables and scope. Since the variable somenode is declared inside of the {} brackets of the for loop, it's scope is local to that for loop. That is to say, it exists during a single iteration of the for loop and then it goes out of scope before the beginning of the next iteration.

Compilers as a general try to be efficient. After the first iteration of the for loop the address 0x7ffc32ff26b0 points to a location in memory that is big enough to store a Node structure. The compiler also knows that this memory is unused (it went out of scope) so it figures it can just re-use the same memory location. In other words, although the structures occupy the same RAM memory, they are all independent and unrelated to each other.

This behaviour is not guaranteed and you should not rely on it, it's simply an attempt from the compiler to keep the final assembled code as simple and efficient as possible.



来源:https://stackoverflow.com/questions/44360004/why-does-the-object-created-without-using-new-operator-gets-the-same-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!