List of new-ed Pointers

This is a discussion on List of new-ed Pointers within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a standard list<my_obj*> and I need to iterate through it and remove all the objects that have ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    List of new-ed Pointers

    Hi,

    I have a standard list<my_obj*> and I need to iterate through it and remove all the objects that have a certain parameter my_obj.time>1.0f.

    I've tried a number of things, but _Crt keeps finding memory leaks. The most recent:
    Code:
    for ( iter1=my_list->begin(); iter1!=my_list->end(); ) {
    	(*iter1)->update(dt);
    	if ( (*iter1)->time>=1.0f ) {
    		iter2 = iter1;
    		++iter2;
    		my_list->remove( *iter1 );
    		//adding "delete *iter" here causes access violation.
    		if (iter2==my_list->end()) break;
    		iter1 = iter2;
    	}
    	else ++iter1;
    }
    The code just keeps getting klugier and kludgier. This can't be the right way to do this.

    Help?
    Thanks,
    Ian

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Delete *iter before removing element from the list.
    I never put signature, but I decided to make an exception.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Huh. Could have sworn I'd tried that, but evidently I didn't. Thanks,

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,291
    std::remove_if() reference

    if you have a compiler that supports a subset of the C++0x standard (specifically lambdas and the redefined auto keyword) you could do something like this:

    Code:
    #include <algorithm>
    std::list<my_obj*> myList;
    auto it = std::remove_if(myList.begin(), myList.end(), [](my_obj* o)
    {
      if (o->time > 1.0f)
      {
        delete o;
        return true;
      }
      else return false;
    });
    std::list<my_obj*> newList(myList.begin(), it);
    newList now contains all elements for which the member 'time' was greater than 1.0f.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    Use smart pointers in your list, ie std::list<std::shared_ptr<my_obj>>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,291
    Quote Originally Posted by Elysia View Post
    Use smart pointers in your list, ie std::list<std::shared_ptr<my_obj>>.
    an excellent suggestion... wish I had thought of it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read-only list of pointers
    By Verdagon in forum C++ Programming
    Replies: 5
    Last Post: 02-10-2011, 07:06 AM
  2. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  4. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21