473,799 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointers and Vectors

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.
vector<*sprite> spvec;
sprite * mysprite new sprite(....);

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.

I understand that a pointer hods 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?

Dec 5 '05 #1
7 2154
enki wrote:
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.
vector<*sprite> spvec;
vector<sprite*> spvec;
sprite * mysprite new sprite(....);
sprite * mysprite = new sprite(....);
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.
Something like:

for (int x = 0; x < val; ++x)
{
spvec.push_back (new sprite(...));
}

?
I understand that a pointer hods a memory location but I am a but
confused with how dynamic memory works.
Why are you dynamically allocating your spites?
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?


C++ separates the memory into three regions:
- automatic storage. This is where local variables go
- static storage. This is where namespace-scope and static local and member
variables go.
- free storage. This is where 'new' takes memory from.

So the data that p points to is located in free storage. new automatically
searches for a free block of memory big enough to hold the type you want to
allocate and gives you a pointer to that location.
As soon as you don't need the value anymore, you need to delete it to give
the allocated memory back to the system.

Dec 5 '05 #2
On 5 Dec 2005 07:10:12 -0800, "enki" <en*****@yahoo. com> wrote:
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_bac k(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!

--
Bob Hairgrove
No**********@Ho me.com
Dec 5 '05 #3

Bob Hairgrove wrote:
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.


On top of that, if you are going to be sharing these pointers with any
other object I would recommend using a smart pointer of some sort
either from boost or home brew.

Dec 5 '05 #4
On 5 Dec 2005 08:03:38 -0800, ro**********@gm ail.com wrote:

Bob Hairgrove wrote:
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.


On top of that, if you are going to be sharing these pointers with any
other object I would recommend using a smart pointer of some sort
either from boost or home brew.


Yes, of course ...
but I thought I wouldn't get into that just yet. ;)

--
Bob Hairgrove
No**********@Ho me.com
Dec 5 '05 #5
Thanks for pointers, oops no pun.
Why are you dynamically allocating your spites?

That is what my book shows. And it seems to work better.

Dec 5 '05 #6
Thaks all... I will see how it all works in my progaram.

Dec 5 '05 #7

"JoeC" <en*****@yahoo. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .

To what/who are you responding? You don't include any of the text of the
message above...
Thanks for pointers, oops no pun.
Why are you dynamically allocating your spites?
(Assuming you're the original poster...)
You started it! :-) Your code was:

sprite * mysprite new sprite(....);

All that was done was to correct the syntax by adding the = sign before the
keyword new. If you didn't want dynamic allocation, then what's the "new"
doing there, and why is it a pointer?

That is what my book shows. And it seems to work better.


_What_ is what your book shows? And what works better than what? Please
quote what you're responding to, and don't just assume we know what you're
talking about.

-Howard

Dec 5 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3117
by: joevandyk | last post by:
Below, I have a class Container that contains a vector. The vector contains pointers to a Base class. When the Container destructor is called, I would like to delete all the objects that the pointers in the vector are pointing to. It works fine, IF I comment out the destructors in Base and Inherited. If I leave the explicit destructors in there, it seg faults. Why is there a difference?
2
2653
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
4
2025
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
18
3063
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary containers and sort those (only pointers have to be copied around then). However, that also means if I...
8
5034
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
20
2132
by: Joe Van Dyk | last post by:
Is there some rule of thumb about when to use pointers to an object and when to use a reference* to an object when a class needs to have objects as data members? Example: class A { B* b_ptr; B b; vector<B*> vector_ptrs;
8
3616
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all we get is this and it doesn't work vector<int*> ones; ones->push_back(7); //this failed to *ones.push_back(7) ones->push_back(8);
23
7423
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
1
2458
nabh4u
by: nabh4u | last post by:
Hi, I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables. I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I have 3 vector variables in Main(). I want the read function to read the values into the vector using the address I send of the vectors.. Sample code: //x.cpp void read(vector <int> a,vector <int> b,vector < vector <int> > c)
160
5728
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.