chaitanya_12789
chaitanya_12789

Reputation: 97

Why is the output as follows for this piece of code?

#include <iostream>
#include<list>
using namespace std;

int main()
{
   list<int *>l;
   
   for(int i=0;i<3;i++)
   {
       int a = i;
       
       l.push_back(&a);
   }
   cout<<endl;
    for(auto i=l.begin();i!=l.end();i++)
    {
        cout<<**i<<" ";
        
    }
   return 0;
}

The output I get is 2 2 2. Is it because, the compiler is creating the new variable at the same address everytime??

Edit1: The code is not doing anything. I just wrote this code as an experiment. Also if the code is written like this:

#include <iostream>
#include<list>
using namespace std;

int main()
{
   list<int *>l;
   
   for(int i=0;i<3;i++)
   {
       int *a = new int;
       *a = i;
       l.push_back(a);
   }
   cout<<endl;
    for(auto i=l.begin();i!=l.end();i++)
    {
        cout<<**i<<" ";
        
    }
   return 0;
}

Now i get the output 0 1 2. What is the difference between the two? Isn't also the pointer destroyed after the loop runs once??

Upvotes: 1

Views: 60

Answers (2)

songyuanyao
songyuanyao

Reputation: 173004

a is a local variable in the scope of for loop, it'll be destroyed when iteration ends. That means the pointers push_backed into l are dangling. Deference on them like **i later leads to UB, anything is possible.

EDIT

What is the difference between the two? Isn't also the pointer destroyed after the loop runs once??

The pointer a itself gets destroyed, just like a with type int in the 1st code snippet; but the int pointed by a is not destroyed. Which makes dereference later like **i well-formed, it'll print out the value of the ints newed. Note that this code has memory leak because the ints newed are never deleteed.

Upvotes: 2

Werner Henze
Werner Henze

Reputation: 16771

Your code has undefined behaviour. l.push_back(&a); stores the address of a loop local variable. That variable is destroyed at the end of the loop body. From this point on accessing the memory behind the stored pointer is undefined behaviour. This is especially true for the **i in the second loop.

With undefined behaviour anything can happen. Please compare Undefined, unspecified and implementation-defined behavior.

Upvotes: 0

Related Questions