Good resource for learning pointers

Discussion in 'C++' started by JoeC, Jan 12, 2007.

  1. JoeC

    JoeC Guest

    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;

    }
    }
    }
     
    JoeC, Jan 12, 2007
    #1
    1. Advertising

  2. JoeC

    Mike Wahler Guest

    "JoeC" <> wrote in message
    news:...
    >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
     
    Mike Wahler, Jan 12, 2007
    #2
    1. Advertising

  3. JoeC

    red floyd Guest

    Mike Wahler wrote:
    > "JoeC" <> wrote 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.
     
    red floyd, Jan 12, 2007
    #3
  4. JoeC

    Marcus Kwok Guest

    JoeC <> wrote:
    > 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/win32cpptut/special/pointers/ch_01.pdf

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Jan 12, 2007
    #4
  5. JoeC

    Daniel T. Guest

    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;
    }
    }
    }
     
    Daniel T., Jan 13, 2007
    #5
  6. JoeC

    Mike Wahler Guest

    "red floyd" <> wrote in message
    news:3wOph.58340$...
    > Mike Wahler wrote:
    >> "JoeC" <> wrote 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
     
    Mike Wahler, Jan 13, 2007
    #6
  7. JoeC

    JoeC Guest

    Mike Wahler wrote:
    > "JoeC" <> wrote in message
    > news:...
    > >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.
     
    JoeC, Jan 14, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dirc Khan-Evans
    Replies:
    1
    Views:
    935
    Karl Seguin
    Oct 17, 2005
  2. avishosh
    Replies:
    2
    Views:
    10,707
    avishosh
    Aug 8, 2004
  3. Replies:
    1
    Views:
    678
    -berlin.de
    Mar 28, 2005
  4. Heinrich Moser
    Replies:
    1
    Views:
    480
    Heinrich Moser
    Mar 27, 2008
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    733
Loading...

Share This Page