What I want to do is create sprites and put them in a vector. I know
that this specific example dosn't realy fit in this group but my sprite
are ponters.
First of all, I would highly recommend starting out here (the C++
FAQ):
http://www.parashift.com/c++-faq-lite/
vector<*sprite>spvec;
sprite * mysprite new sprite(....);
You need to write:
sprite * mysprite = new sprite(....);
When you are done with the sprite, you must destroy the object,
otherwise you will have a memory leak:
delete mysprite;
Also, it is a good idea to set the pointer to 0 in order to avoid a
double deletion bug:
mysprite = 0;
when I create a sprite \:
spvec.push_back(mysprite);
basically I want to do somthing like this:
switch(val){
case 1:
sprite * mysprite new sprite(...);
case2....
basically I want to create and destroy an unknown number of sprites
stored and controlled in a vector, is this a good way to go.
If you can copy your sprite objects, and the overhead of copying
resources isn't too large, it would be better to keep them in a vector
of objects. That way, you won't need to manage the lifetimes with new
and delete.
However, if you have polymorphic sprites and need to keep sprites of
the different inherited types in one container, you must use pointers
of the common base class. This also means making sure that your base
class has a virtual destructor because you will have to call delete on
the base class pointer, and that would cause undefined behavior
otherwise.
I understand that a pointer holds a memory location but I am a but
confused with how dynamic memory works.
int x;
int px;
x =10;
px = &x;
With int * p = new int;
p points to the data but where is the original data stored much like in
t x and px just points to x?
You really need to read the FAQ and a good text book.
Creating an int (or anything else) with new allocates a region of
memory on the free store, or process heap (assuming that new and
delete haven't been overloaded to do something else). This is called
"dynamic memory allocation". Any of your other declarations above will
allocate the memory either in some unspecified segment of RAM decided
by the compiler and the operating system, if it is a global or static
variable, or else the variable is allocated locally on the stack. The
C++ standard, however, doesn't specify what a stack is or that local
variables must be allocated exactly that way. It is implementation
defined how the compiler does it. At any rate, the moment you leave
the block where the local variable is declared, it goes out of scope
(i.e. ceases to exist) and the memory it occupied is freed by the
compiler automatically -- that's why they are also called "automatic"
variables. With dynamic allocation, the object continues to live
beyond the local scope. However, if the pointer to that object was
declared locally, the pointer goes out of scope, and if you didn't
delete the memory before that happens, you would have a memory leak --
unless you had copied the pointer into your vector, of course, in
which case you delete it later, and there is no leak.
If your program allocates objects dynamically, it is also responsible
for destroying the objects in an orderly fashion. For that reason,
it's best to avoid using new and delete if you can. That's why it
might be a very good idea to keep objects instead of pointers in your
vector.
Good luck -- you have lots to learn, but it will hopefully be fun in
the process!