Sign in to follow this  

Pointer problems

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] == NULL)
        {
           Monster* New_Monster = Monsters[rand()%A_Map->Set.y+A_Map->Set.x];
           Curr_Monsters[i] = &New_Monster; 
        };    
    };  

this dosnt:
  for(int i = 0; i < 3; i++)
  {
        if(Curr_Monsters[i] == NULL)
        {
           Curr_Monsters[i] = &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]

Share this post


Link to post
Share on other sites
The first one assigns Curr_Monsters[i] 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.

Share this post


Link to post
Share on other sites
Specifically,

Curr_Monsters[i] was set to the address of a local variable, which has gone out of scope once the functions exits. Essentially, a dangling pointer situation.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
they addressed your error but on an unrelated note, those semicolons after your brackets are pointless

Share this post


Link to post
Share on other sites
ok im now calling the loop like this

for(int i = 0; i < 3; i++)
{
if(Curr_Monsters[i] == NULL)
{
New_Monster = new Monster(Monsters[rand()%A_Map->Set.y+A_Map->Set.x]->LBL.c_str(),Skills);
Curr_Monsters[i] = &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)

Share this post


Link to post
Share on other sites
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[i] = &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]]?

Share this post


Link to post
Share on other sites
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[i] == 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[i]; // So this is an address to a monster
Curr_Monsters[i] = New_Monster; // this is correct
Curr_Monsters[i] = &Monsters[rand()%A_Map->Set.y+A_Map->Set.x]; // this is correct too
};
};


Share this post


Link to post
Share on other sites
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[i] == NULL)
{
Curr_Monsters[i] = &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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this