0

I have a class in which I have declared the array of list as a data member.

list <int> **listOfNodes;

I have allocated space for the pointers to lists in the constructor of the class as follows (here 'v' is the no. of lists I want, passed to the constructor as an argument.)

listOfNodes=new list<int>* [v];
for (int i = 0; i < v; ++i)
{
    list<int> temp;
    listOfNodes[i]=&temp;  //declaring a new list and making the list pointer point to it
}

Now I have the following code inside the function for taking the input from the user and adding it to the corresponding list. For example, if user enters 2 5, I need to push_back a new entry 5 into the list with index 2, i.e, the list pointed to by listOfNodes[2].

int u,v;
cin>>u>>v;
(*(listOfNodes[u])).push_back(v);

But, somehow my code crashes on execution. Can someone please point out what I might be doing wrong.

Shobhit
  • 773
  • 7
  • 20
  • 1
    What is wrong is that you are using a pointer of pointers to `std::list`. I suggest using `std::vector`(s) instead. – juanchopanza May 05 '13 at 10:24

2 Answers2

2
for (int i = 0; i < v; ++i)
{
    list<int> temp;
    listOfNodes[i]=&temp;  
} // <- each temp is destructed here.

your temp lists are automatically allocated. That means once you go out of the loop scope they are destructed. Now your listOfNodes[i] is pointing to some destructed memory (probably they are pointing to the same location because the compiler keeps allocating temp at the same address each time. Still invalid though.).

You should instead do it like

for (int i = 0; i < v; ++i)
{
    listOfNodes[i] = new list<int>;  
}

And don't forget to delete the dynmaically allocated memory. You should probably just use a list/vector of lists or use a list of smart pointers instead.

Community
  • 1
  • 1
stardust
  • 5,918
  • 1
  • 18
  • 20
  • 1
    Thank you very much @Named. I was struggling with this for few hours. And yes, I also thought of using a vector of lists later but wanted to figure out what was wrong with this code. Would also read about smart pointers. Thanks again for such a quick reply and added advices. :) My first question on SO and I am highly impressed (in addition to the respect I have for SO's already asked quest. for helping me out billions of times). Would definitely have upvoted had I had some reputation. – Shobhit May 05 '13 at 10:38
  • @Shobhit :) No problem man. I am glad it helped. And don't worry about the upvote. I have got two already. It is enough :) – stardust May 05 '13 at 10:40
  • Who keeps downvoting my answers? I really don't get it. Could you please mention why? – stardust May 05 '13 at 10:54
  • @Named: If you are regularly downvoted without commentary on posts without obvious problems (as this one, +1) and you think there is a pattern, you can contact SO officials and ask them to check if there are voting irregularities. But do so only if it exceeds an acceptable level. – bitmask May 05 '13 at 11:10
  • @bitmask Thank you. I think I should. The reason I am suspecting it because there is no comment left with the down-vote. I will wait and see if it passes, otherwise I will contact them. Thanks again. – stardust May 05 '13 at 11:13
2

Avoid mixing C++ containers (list,vector,set,...) with C containers (plain array). If you want to keep a number of lists use vectors:

std::vector<std::vector<std::list<int>>> nodes;

instead of

std::list<int>** listOfNodes;

It is very cumbersome to keep track of the list instances by hand (exactly what you experienced). So let the vector (or whatever else you deem fit, an std::array might easily be more appropriate depending on your situation) take care of handling the list instances.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • Thank you @bitmask for the useful suggestion. Yes, I later did think of using a vector of lists as suggested by you, but wanted to figure out the loophole in my initial code. And once again, Would have definitely upvoted had I had some reputation. – Shobhit May 05 '13 at 10:46
  • @rubenvb: I know it's a weird formulation, but I wanted to point out that arrays are what you (have to) use in C if you want a vector-style container. In that regard, `int[]` is not an idiomatic C++ container (but the metaphor shouldn't be stretched too much, I agree). – bitmask May 05 '13 at 11:03