Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer problems

Started by raptorstrike Dec 30, 2004 at 10:26 PM 10 replies 1k views
Original Post
raptorstrike
raptorstrike
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 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
SiCrane
SiCrane
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.
VinnyC
VinnyC
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.
fastbit
fastbit
your code may be something like this:
Curr_Monsters = New_Monster;

none
raptorstrike
raptorstrike
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
Oluseyi
Oluseyi
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]]?
Xai
Xai
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        };        };  
raptorstrike
raptorstrike
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
Zahlman
Zahlman
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.
raptorstrike
raptorstrike
amazingly

that dose me no good. I already have a random array picking algorithum as you can see above my problem now is that i need seprate monsters i COULD just have it so that you never have the same monster twice but id like to avoid that if your post was an attemp to show me a way that i will not get doubles thank you but im still looking for another way

thanks for posting [smile]
____"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Zahlman
Zahlman
How exactly do you propose to have "separate monsters" if you allow to have "the same monster twice"? Do you want to treat the contents of your array as prototypes, or something? If so, you may want to double-check your copy constructor (and assignment operator, and destructor...)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.