Thread: Problem with pointers

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Problem with pointers

    I'm doing my best to try and understand pointers but I'm getting a segfault in my program and I think it's because of how I'm trying to use pointers.

    I have an iterator iterating over a list of Requester* and activeRequesters is that list of pointers. Then I dereference the first iterator which should give me a Requester*. Then I have an iterator iterating over a list of Request* which are contained in the Requester. Again, I dereference the iterator to get the pointer. Then I try and print track which is an integer contained by a Request.

    Could my problem also be in how I'm iterating over the list? I've seen it sometimes done with ++it instead of it++, I'm not sure what the difference is.


    Code:
    for(list<Requester*>::iterator it = activeRequesters.begin();it!=activeRequesters.end();it++)
    	{
    		Requester* cur = *it;
    		cout << "Requester:" << endl;
    		for(list<Request*>::iterator it2 = cur->requests.begin();it2!=cur->requests.end();it2++)
    		{
    			Request* cur2 = *it2;
    			cout << cur2->track << endl;
    		}
    	}

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'm not really sure what's wrong with that; it looks okay to me. The only thing you might want to watch out for is NULL objects. If a Requester's requests object is NULL, for example, that will cause a segmentation fault.

    Could my problem also be in how I'm iterating over the list? I've seen it sometimes done with ++it instead of it++, I'm not sure what the difference is.
    ++it is more efficient, which is why some people use it. Personally, I just use it++ all of the time out of habit.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Why is it more efficient, what does it mean?
    Last edited by jcafaro10; 01-27-2009 at 02:07 PM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcafaro10 View Post
    Why is it more efficient, what does it mean?
    It is MARGINALLY more efficient if you have a situation where (for example) the increment is done on an iterator. The way that operator++ works for pre- and post-increment (postincrement is "it++" and pre-incrment is "++it") is that post-increment returns the current value, then increments. This means that the original value needs to be held in a copy, before we can increment the object. The act of making a copy before returing it will of course take some time. If the compiler is clever, it will realize that the value is never used, and remove the copying process, but it may not understand enough of what's going on to be able to eliminate the copy.

    A second reason is that the way that linker (and compiler) tells pre- and post-increment apart is that postincrement has a dummy integer argument added to the operator++ - so it is operator++(int), whilst the pre-increment is operator++(void). So passing an extra argument is also involved - again, a good compiler will inline the operator and remove the unused argument. But it's possible that it makes a tiny difference.

    If you have a VERY long [1] list/vector that you iterate through, and you do very simple operations on the objects, then it may make a small difference what you choose.

    [1] Millions or more elements.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM