Sign in to follow this  
Crazyfool

Pointers and addresses

Recommended Posts

Hi, I have a class that holds all the objects of a class, called Npc. Then I have a 2d array of Npc pointers, which represent where the Npc's are at. So when I spawn an Npc, I do this little tidbit:
npcs.push_back(n);
manager->world->x[abs(n.x)].z[n.z].npc = &npcs[npcs.size() - 1];
Basically npcs is a vector of Npc objects, and then I try setting the grid's address at nx/nz to the address of the lastly added object of Npcs. I am pretty sure somethings going wrong here. and as a side note the abs(n.x) is mostly due to personal preference of the grid in my imagination and working with negative numbers as it goes on. Dont ask me why =/

Share this post


Link to post
Share on other sites
Well, I can't tell you what's going wrong without some description of what's going wrong or some code to see exactly how your manager object is structured. However, you could replace
&npcs[npcs.size() - 1]
with
&npcs.back()
which would make your code cleaner, more intuitive, and more managable should you decide to change the type of container represented by npcs.

Share this post


Link to post
Share on other sites
Well, depends on what you mean "wrong". First of all, for instance, this line:

manager->world->x[abs(n.x)].z[n.z].npc

makes me want to take a bath. It compiles and runs but it's just...bad. Better have a World::SetGrid(x,y,npc) function or something like that.

Other than that, you have to realize that std::vector is free to move elements into memory as you add or remove items. So what is now a valid address may not be later. Storing the address of an std::vector item for later use is wrong, unless the vector never resizes, which is not your case. A correct solution would be to make npcs a vector of pointers to NPC.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
Well, depends on what you mean "wrong". First of all, for instance, this line:

manager->world->x[abs(n.x)].z[n.z].npc

makes me want to take a bath. It compiles and runs but it's just...bad. Better have a World::SetGrid(x,y,npc) function or something like that.

Other than that, you have to realize that std::vector is free to move elements into memory as you add or remove items. So what is now a valid address may not be later. Storing the address of an std::vector item for later use is wrong, unless the vector never resizes, which is not your case. A correct solution would be to make npcs was a vector of pointers to NPC.


Thats the issue!

Sometimes it works and sometimes it doesnt, so it baffled me completely. I am terribly sorry for making you guys guess but my code is getting pretty beastly and its hard to tell what I need to include.

What do you reccommend I use? Would std::list be a better option? or should I use a slightly abstracted array?

Thanks so much!

Share this post


Link to post
Share on other sites

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