Jump to content
  • Advertisement
Sign in to follow this  

pointer questions

This topic is 3272 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

I wanted to check if a few things I'm doing with C++ pointers is alright.

struct location {
  float xpoint, ypoint;

location *parent;
int root;

location make_location(float xpointer, float ypointer) {
  location loc = {xpointer, ypointer};
  return loc;

vector<location> openlocations;
location currentsquare;

//two locations are added to openlocations. One location is made the parent of the other



//starting from one square the loop iterates from parent to parent
//putting them into waypoint until the root is reached
waypoint.push_back(make_location(currentsquare.xpoint, currentsquare.ypoint));

[Edited by - Jarwulf on December 2, 2009 7:15:13 PM]

Share this post

Link to post
Share on other sites
Did you have a question? You don't really say what you want to do, but I can see a few potential issues. It looks like you're trying to implement a sort of linked list but putting the nodes into a vector instead of allocating the memory so I'll base my comments on that assumption.

First, your code seems incomplete. You never assign to 'root' at any point in your code and your make_location function would initialize all of the 'root' and 'parent' elements to 0 so you would have an infinite loop which would probably crash at some point, likely immediately.

Second, your line of code:

is definitely incorrect. size returns the size of the container and the container can only access elements from 0 to size()-1, inclusive. When size() is greater than or equal to 2 this will work (but only if the size is 2+):


Third, if you are trying to implement a sort of linked list, then you would need to do the above line of code after every time you push_back a location object, not after every 2. As it is, your code will link one object to a second, but the second object won't be linked to anything.

Fourth, std::vector will not remain in the same location in memory if you push_back and the vector runs out of capacity. This means that all of your pointers will probably become incorrect and point at the wrong thing. This would work if you reserved enough capacity in your vector before use, but you don't show that so as is, your code will probably fail.

I can probably go on.

If you tell us what you are really trying to do we can maybe come up with a better solution. That solution probably won't involve pointers (directly) though. If you just want to learn more about pointers then at least try and implement a real linked list and not this psuedo-linked list.

Share this post

Link to post
Share on other sites
First: your make_location function could be a constructor for your location constructor.

Your make_location function should initialize all the attributes of the structure (for debugging if nothing else). It's sort of dangerous to have an uninitialized pointer.

The line:


is bad for 2 reasons:
1. openlocations[openlocations.size()] doesn't exist. You'll get a runtime error. vectors are zero-based indexed. If you have a vector with 2 elements, those are located at openlocations[0] and openlocations[1]. The line above tries to load something into openlocations[2] ( openlocations.size()==2, right? )

2. "&openlocations[(openlocations.size()-1)]" is the address of the last element in the vector. vector elements can be pushed and popped so that address may end up pointing to a different structure than you expect; worse - if you pop that last element, the pointer will be invalid and you'll likely get a memory access error.

Your while loop is generally okay, but it will either never be executed (if root==1 going into it), or run forever as the value of root is never changed inside the loop, so "while(root !=1)" will always be true.

If you just want to iterate through the vector to create the waypoints, look at using vector<location>::iterator.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!