Pointers in a list not having correct data

Started by
5 comments, last by spunkybuttockszc 19 years, 1 month ago
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.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
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.
The code you've posted doesn't make any sense. What is it that you are trying to accomplish?
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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 allocatedfor (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
Quote:Original post by Palidine
STL list or STL vector
I concur.
Rob Loach [Website] [Projects] [Contact]
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.

This topic is closed to new replies.

Advertisement