473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Good resource for learning pointers

I understand the basics of pointers, they point to memory locations. I
would like to know resources for learning all about poters. I am
having some problems erasing elements of pointers from a vector. I
wold like to know where I can get some in depth information on how to
use pointers in various situations. It seems that all most books have
to say about pointers is that they point to memory locations. Some
even say they are read/write iterators for arrays. I am using pointers
refrences and handles, most of the time I get the syntax correct but I
have a problem in a complex program and I cant figure out what the
problem is:

I created a handle to contor my dynamicly binded units and I need
pointer to these handles to work in the space holder object in my map
manager for this game.

bool spaceholder::kill(){
std::vector<hunit*>::iterator itr = u.begin();
if(!u.empty()){
while(itr != u.end()){
if((*itr)->marked()){
itr = u.erase(itr);
kill = true;
}++itr;

}
}
}

Jan 12 '07 #1
6 1474

"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
>I understand the basics of pointers, they point to memory locations.
The contain address values which represent memory locations.
I
would like to know resources for learning all about poters.
I find this one reasonable:
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
Note: it's in the context of C, but it works the
same as C++, except for the 'void*' automatic
conversion.

But after looking at your code, I don't think your
trouble (if I've correctly guessed what it is),
has to do with pointers, but with iterators.
I am
having some problems erasing elements of pointers from a vector.
Specifically what problems?
I
wold like to know where I can get some in depth information on how to
use pointers in various situations.
Good textbooks (see www.accu.org) or see link above.
It seems that all most books have
to say about pointers is that they point to memory locations.
Which books?
Some
even say they are read/write iterators for arrays.
That's a reasonable description of one thing they
could be used for.
I am using pointers
refrences and handles,
'handle' is a term not defined by the C++ language, but
the 'handle' abstraction is often implemented as a pointer.
most of the time I get the syntax correct but I
have a problem in a complex program and I cant figure out what the
problem is:
You still haven't described your problem, but looking at your code,
and making a couple assumptions, I can guess what the trouble is.
>
I created a handle to contor my dynamicly binded units and I need
pointer to these handles to work in the space holder object in my map
manager for this game.

bool spaceholder::kill(){
std::vector<hunit*>::iterator itr = u.begin();
if(!u.empty()){
while(itr != u.end()){
Aside:
If begin() == end() then the container is empty,
so the test for !empty() is redundant.
if((*itr)->marked()){
Is type 'hunit' an iterator or pointer to a struct/class, or does it
overload the '->' operator? If not, this syntax is incorrect.
I'll go ahead and assume that the answer to this question is yes.
itr = u.erase(itr);
This is probably your problem. vector.erase() returns an
iterator to the element beyond the one erased. If you've
just erased the last element, this returned iterator will
be 'end()', in which case ...
kill = true;
}++itr;
.... this line would cause 'itr' to increment past 'end()',
producing undefined behavior. Was the problem a crash,
or 'segfault', or 'access violation' or similar?
>
}
}
}
Try:

bool spaceholder::kill(){
std::vector<hunit*>::iterator itr = u.begin();
while(itr != u.end()){
if((*itr)->marked()){
itr = u.erase(itr);
kill = true;
continue; /* new line */
}++itr;
}
}

If this does not solve your problem, you'll need to give
more specific information, ideally including a complete,
compilable example which exhibits the problem.

-Mike
Jan 12 '07 #2
Mike Wahler wrote:
"JoeC" <en*****@yahoo.comwrote in message
[redacted]
>>
bool spaceholder::kill(){
std::vector<hunit*>::iterator itr = u.begin();

[redacted]
> if((*itr)->marked()){

Is type 'hunit' an iterator or pointer to a struct/class, or does it
overload the '->' operator? If not, this syntax is incorrect.
I'll go ahead and assume that the answer to this question is yes.
You're mistaken. itr is an iterator into a vector of hunit*. Therefore
(*itr) refer to an hunit*, and by definition, -is defined. The syntax
is *always* correct here.
Jan 12 '07 #3
JoeC <en*****@yahoo.comwrote:
I understand the basics of pointers, they point to memory locations. I
would like to know resources for learning all about poters. I am
having some problems erasing elements of pointers from a vector. I
wold like to know where I can get some in depth information on how to
use pointers in various situations.
Alf P. Steinbach's tutorial has lots of great information on pointers:

http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 12 '07 #4
JoeC wrote:
bool spaceholder::kill(){
std::vector<hunit*>::iterator itr = u.begin();
if(!u.empty()){
while(itr != u.end()){
if((*itr)->marked()){
itr = u.erase(itr);
kill = true;
}++itr;

}
}
}
bool spaceholder::kill() {
std::vector<hunit*>::iterator itr = u.begin();
while ( itr != u.end() ) {
if ( (*itr)->marked() ) {
// you probably need to do something here to destroy the
hunit contained,
// unless some other pointer also points to is
itr = u.erase( itr );
kill = true;
}
else {
++itr;
}
}
}

Jan 13 '07 #5

"red floyd" <no*****@here.dudewrote in message
news:3w*******************@newssvr13.news.prodigy. net...
Mike Wahler wrote:
>"JoeC" <en*****@yahoo.comwrote in message [redacted]
>>>
bool spaceholder::kill(){
std::vector<hunit*>::iterator itr = u.begin();
[redacted]
>> if((*itr)->marked()){

Is type 'hunit' an iterator or pointer to a struct/class, or does it
overload the '->' operator? If not, this syntax is incorrect.
I'll go ahead and assume that the answer to this question is yes.

You're mistaken. itr is an iterator into a vector of hunit*. Therefore
(*itr) refer to an hunit*, and by definition, -is defined. The syntax
is *always* correct here.
You're right, of course. Joe, sorry for the misinformation.

-Mike
Jan 13 '07 #6

Mike Wahler wrote:
"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I understand the basics of pointers, they point to memory locations.

The contain address values which represent memory locations.
I
would like to know resources for learning all about poters.

I find this one reasonable:
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
Note: it's in the context of C, but it works the
same as C++, except for the 'void*' automatic
conversion.
Thanks, I will check it out. I have a specific problem that I think I
fixed but I would like to learn more about pointers and refrences
because there are times when I have trouble with them.

Jan 14 '07 #7

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

Similar topics

29
by: Cheng Mo | last post by:
Recently I happens to read some sourcecode which has different style from my previous experience. In this style, all functions return a RETURN_TYPE_T which is defined as typedef unsigned int...
60
by: K. G. Suarez | last post by:
Hello everyone. I am new to programming and my uncle gave me a copy of "C For Dummies 2nd Edition". I am up to chapter 9 right now. He probably saw me struggling with "The C Programming...
10
by: Harley | last post by:
Hello, I was VERY blessed with a Christmas gift of visual studio .net from a man I hardly know who had heard of my plans of software developement. So I am probably the only person in the world who...
3
by: newbie | last post by:
I'm fairly comfy with vb6 and would like to step right into vb.net. Anyone have suggestions for a really good beginner program to write? Thanks!
17
by: I_got_questions? | last post by:
I just started c programming. I want to migrate to c++. I know a little bit about class/inheritance. I am asking for good books to read. Elementary will be good. Thanks for any comments
9
by: Tom the Canuck | last post by:
A while back I was playing with C++ and made a simple program with a WAV file as a resource. It worked well and was easy to make. I then went on to try this with VB. I had problems. Can this be...
6
by: bambooforest | last post by:
Hi all, I'm from a Linguistics background and am new(er) to programming. Could someone recommend a book or resource that teaches programming aspects with Python? Python I hear is a very...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
75
by: Amkcoder | last post by:
http://amkcoder.fileave.com/L_BitWise.zip http://amkcoder.fileave.com/L_ptr2.zip
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.