Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

Pointers in a list not having correct data

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

How can I make this work:
MyClass* obj;

obj = &MyClass(1);
obj++;
obj = &MyClass(2);
obj++;
...
When I run this, the first obj (obj[0]) is the only one that ever gets changed and it gets overwritten with the other calls; the others are empty.

Share this post


Link to post
Share on other sites
Advertisement
That's because you're only updating the pointer's address and not the data it's pointing to.
Try this instead:
*obj++ = MyClass(1);
*obj++ = MyClass(2);
Oh, and don't forget to allocate some space for the array somewhere.

Share this post


Link to post
Share on other sites
Holy something!

Sfpiano: Those objects are temporary, they don't exist any more after that line of code finishs. Using their address for anything is bad.


MyClass* obj; // obj is a pointer. Right now, it's pointing to some random location in memory.

// Set's obj to the address of a temporary MyClass object.
obj = &MyClass(1);
// At this point, that temporary object doesn't exist any more, and obj is pointing to garbage.
obj++; // This moves obj forward in memory by the same number of bytes as a MyClass object had. Not only is this not a valid object, but this was _never_ at any point a valid MyClass object.
// This sets obj to another temporary object. It could very easily have the same address as the last one. Since you're now setting the address, why the heck you you set the pointer if you just incrimented it? What's the point?
obj = &MyClass(2);
// Again, obj is pointing to garbage.
obj++; // And again, this doesn't make any sense in the context of this code.



doynax: obj was never allocated, trying to assign stuff to it is also bad. Nevermind, you commented on that.

I'm going to agree and say that that this doesn't make a whole lot of sense.

Share this post


Link to post
Share on other sites
yea, I think you are trying for something like this:


MyClass *objects[20];

for (int i = 0; i < 20; ++i )
{
objects = new Myclass(i);
}

//do some stuff with your objects

//absolutely make sure to free the memory you've allocated
for (int i = 0; i < 20; ++i )
{
delete objects;
}




if you're looking for a variable amount of objects to be used in your objects list, look into the STL list or STL vector classes. If you are not already familliar with the concecpt of linked lists, I'd suggest looking it up and writing your own for the learning process.

-me

Share this post


Link to post
Share on other sites
Maybe you should give us information as to *why* you need to do what you're doing. This way we could help you accordingly. Because right now, as people said, your code doesn't make much sense, so we really don't know what to tell you.

Just a thought.

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!