Pointer problems

Started by
10 comments, last by Zahlman 19 years, 3 months ago
ok i dont know why this isnt working cause it looks fine to me but maybe you guys can help me out this crashes my program:

 for(int i = 0; i < 3; i++)
    {
        if(Curr_Monsters == NULL)
        {
           Monster* New_Monster = Monsters[rand()%A_Map->Set.y+A_Map->Set.x];
           Curr_Monsters = &New_Monster; 
        };    
    };  

this dosnt:

  for(int i = 0; i < 3; i++)
  {
        if(Curr_Monsters == NULL)
        {
           Curr_Monsters = &Monsters[rand()%A_Map->Set.y+A_Map->Set.x];
        };    
  }; 

Curr_Monsters is a std::vector<Monster**> and Monsters is a Monster* [] (or Monster** but its an array for clarity) thanks for your help[smile]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
The first one assigns Curr_Monsters to be an address on the stack, the address of the New_Monster local variable. If you try using that later, then chances are you'll dereference something you don't want to. Boom.
Specifically,

Curr_Monsters was set to the address of a local variable, which has gone out of scope once the functions exits. Essentially, a dangling pointer situation.
they addressed your error but on an unrelated note, those semicolons after your brackets are pointless
your code may be something like this:
Curr_Monsters = New_Monster;

none
ok im now calling the loop like this
 for(int i = 0; i < 3; i++)    {        if(Curr_Monsters == NULL)        {           New_Monster = new Monster(Monsters[rand()%A_Map->Set.y+A_Map->Set.x]->LBL.c_str(),Skills);           Curr_Monsters = &New_Monster;        };        }; 

the reason for me calling it like this is so that there are three seprate monsters but it still seems to be treating the 3 seprate monsters as the same one for example when i decrease one monsters health they all take a hit. Is there a way to make them 3 individual monsters without having to use the Monster class raw (Monster New_Monster instead of Monster* New_Monster)
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
New_Monster = new Monster(Monsters[rand()%A_Map->Set.y+A_Map->Set.x]->LBL.c_str(),Skills);
Because you dynamically create this instance and then leave scope, and because it's unlikely that you're calling delete on it, you've just sprung a memory leak. Nice going, champ!

What, exactly, do you feel is so wrong with
Curr_Monsters = &Monsters[rand()%A_Map->Set.y+A_Map->Set.x];
? Why isn't it enough to have the indices to three of your existing monsters (by simply assigning the reference) in Curr_Monster[[0 - 2]]?
man, you CANNOT return the address of a LOCAL varialbe, period ...

if you do this:

SomeType someVariable;

// NO MATTER WHAT YOU PUT HERE
// you cannot return this:

return &someVariable

because it's going to go out of scope ...

 for(int i = 0; i < 3; i++)    {        if(Curr_Monsters == NULL)        {           Monster* New_Monster; // this is a variable that holds the address of a monster.           Monsters[rand()%A_Map->Set.y+A_Map->Set.x]; // this is a monster           &Monsters[rand()%A_Map->Set.y+A_Map->Set.x]; // this is the address of a monster           New_Monster = &Monsters[rand()%A_Map->Set.y+A_Map->Set.x]; // so this is correct           Curr_Monsters;  // I assume this is an array of addresses to monsters           Curr_Monsters;  // So this is an address to a monster           Curr_Monsters = New_Monster; // this is correct           Curr_Monsters = &Monsters[rand()%A_Map->Set.y+A_Map->Set.x]; // this is correct too        };        };  
Oh acctually i forgot to tell you that all of the variables there are now global so there isnt a problem with that any more [grin]

in response to Oluseyi:

because if i do this:

for(int i = 0; i < 3; i++)
{
if(Curr_Monsters == NULL)
{
Curr_Monsters = &Monsters[rand()%A_Map->Set.y+A_Map->Set.x];
};
};

if by some chance &Monsters[rand()%A_Map->Set.y+A_Map->Set.x] gives me the same value twice then i have to pointers to the same data for example sapose that it returned 0 twice

((*Curr_Monsters[0])->Health -= 1)

then

all the monsters of the same type as Curr_Monsters[0] will -= 1

i need seprate monsters i could make it so that that you will never get the same 2 monsters but that is a last resourt
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Q. How do I select n random, unique items from an array of k items?

A. Shuffle it (permute it randomly) and select the first n items, or equivalently, "shuffle on demand" by the following process:
Repeat with i from 1 to n:  Select a random element from 0 to k-i inclusive.  Copy the k-i'th element over the element just selected.


Q. What if I must not alter the array?

A. Create a temporary array of array indices, shuffle it in the same way, and use it to select items from the original array.

This topic is closed to new replies.

Advertisement