I want a dynamic container that I can add items to or remove items from and that will allocate memory automatically, unlike an array. But I also want something that I can modify the elements of, and I don't mean replace, but actually run methods and functions of the classes stored in the container. This doesn't work with an array or a list as far as I have tested.

Ideally the second iteration of values would all be one more than the first time, but they are not.A forward list would work for me, as I have a program that every frame loops though each object (begin to end) and runs a method which will modify values in that class. As long as I can remove an object at any point in the container, I don't need to access elements randomly, only from begining to end.

In that example you are taking the unknown from the iterator by value.

Use a reference instead, currently you are copying it and ediring the copy. Im not sure if there are still problems with the std containers as i have little experience with them, just making sure thats not the problem.

You have several options to fix the problem the best is probably to use smart pointers in your container. However, if you go back to your original code you can use the iterators directly and everything should work fine. i.e.

So right now I'm using alleightup's suggestion and it's working well. Hopefully there isn't any memory loss, it'll be a little bit before I have the actual program ready to test and can see for myself.

Currently my question is how can I remove an element?Something along the lines of this:

for(auto& it : lifeforms)
{
if(it->food < 1)
lifeforms.remove(it);
}

Does not work.The program crashes.

How do I remove an element?(while freeing its memory, as I only need the elements that are in the list)

Err, I probably should have changed the naming a bit so it was more clear.. The auto type in the examples I gave will become references to the dereferenced iterator. So, 'auto& it' actually becomes 'whoKnows& it = *impliedIterator;' I should have changed "it" to be something like "yourObjRef" since the iterator is now implied and hidden.

So right now I'm using alleightup's suggestion and it's working well. Hopefully there isn't any memory loss, it'll be a little bit before I have the actual program ready to test and can see for myself.

Currently my question is how can I remove an element?
Something along the lines of this:

for(auto& it : lifeforms)
{
if(it->food < 1)
lifeforms.remove(it);
}

Does not work.
The program crashes.

How do I remove an element?
(while freeing its memory, as I only need the elements that are in the list)

Given the use of std::forward_list, you are limited to pretty much a single solution for the above:

lifeforms.remove_if( []( lifeform& n ){ return n.food < 1; } );

Sorry, you can't iterate and remove from a forward list directly, for that you need to go back to std::list.

Err, I probably should have changed the naming a bit so it was more clear.. The auto type in the examples I gave will become references to the dereferenced iterator. So, 'auto& it' actually becomes 'whoKnows& it = *impliedIterator;' I should have changed "it" to be something like "yourObjRef" since the iterator is now implied and hidden.

So right now I'm using alleightup's suggestion and it's working well. Hopefully there isn't any memory loss, it'll be a little bit before I have the actual program ready to test and can see for myself.

Currently my question is how can I remove an element?Something along the lines of this:

for(auto& it : lifeforms)
{
if(it->food < 1)
lifeforms.remove(it);
}

Does not work.The program crashes.

How do I remove an element?(while freeing its memory, as I only need the elements that are in the list)

Given the use of std::forward_list, you are limited to pretty much a single solution for the above:

lifeforms.remove_if( []( lifeform& n ){ return n.food < 1; } );

Sorry, you can't iterate and remove from a forward list directly, for that you need to go back to std::list.

So that seems like something I only need to run once per frame, and not once per organism per frame, correct?e.g.

In this case you are back to using the actual iterators instead of hiding them, and you are using a double linked list (i.e. std::list) which allows erase to function without being a secondary iteration task.

I'll change to a regular list if it's a problem.But will that cause a noticable drop in preformance?(note that I have thousands of objects in a list each having their functions run and values modifyed and accessed per frame)

I'll change to a regular list if it's a problem.
But will that cause a noticable drop in preformance?
(note that I have thousands of objects in a list each having their functions run and values modifyed and accessed per frame)

Sorry about the typo, oops.

For the most part, you should end up with better performance given the way you are using the list. Individual operations are a little more expensive but that is pretty much trivial and can be ignored. You should not be worrying about the performance at this level, it is a minor issue compared to what your other functions are likely going to be doing.

I switched my code to use a regular list, and it's working alright.But I guess I have a slightly different problem now: memory leaks.I kinda figured when elements were removed from the list they weren't freed from memory, so I tried something like this:

This seems to cut down on the memory leaks, however I get a strage problem that the number of elements in the list is drastically differnet from the lists size, once the programs been running for a bit and many elements have been removed:

Why do you have a list of pointers to cells anyway, why not just a list of cells?

Read the OP.That was the origional problem.I needed to modify the elements of the list and run functions on them.If I used pointers then I'd just get a COPY of the object, and the modifications would not stick.

Why are you using free? You must 'delete' what you 'new'. As this is clearly C++ code, you should not be using malloc or free.

Crashes the program.I try "delete &old;" after the erase and it doesn't work.I've tried a few other things as well.

Why do you have a void pointer? Those are very very seldom useful in C++. Call delete through a pointer to the right type, or base class type.

Doesn't seem to make a difference.

Those are the obvious problems to fix first.

Can't really fix problems concerning how "common" or "correct" something is if the program doesn't even stay open.At least with the things I was doing it was working, it just had a huge memory leak.

Why do you have a list of pointers to cells anyway, why not just a list of cells?

Read the OP.
That was the origional problem.
I needed to modify the elements of the list and run functions on them.
If I used pointers then I'd just get a COPY of the object, and the modifications would not stick.

Why are you using free? You must 'delete' what you 'new'. As this is clearly C++ code, you should not be using malloc or free.

Crashes the program.
I try "delete &old;" after the erase and it doesn't work.
I've tried a few other things as well.

Why do you have a void pointer? Those are very very seldom useful in C++. Call delete through a pointer to the right type, or base class type.

Doesn't seem to make a difference.

Those are the obvious problems to fix first.

Can't really fix problems concerning how "common" or "correct" something is if the program doesn't even stay open.
At least with the things I was doing it was working, it just had a huge memory leak.

Oh now I see what you meant earlier. You were in fact mistaken right from your very first post. You can of course modify items in a list, and no I certainly don't mean "replace". You can make whatever changes to the items in a list that you like. Same for a vector. This is good news because it means you probably can forget about storing pointers to objects afterall, and have the containers manage the memory for you.

Post the code for whatever "tests" you did if you want to find out what led to your incorrect conclusion.

You are using delete wrong. You can't delete the address of a local or other variable, you must pass just the pointer varible itself to delete.

There are on if's or but's about it either. If you allocated using 'new' then you MUST destroy using 'delete', end of story.

Deleting through a pointer to the correct type or base type will indeed make a very big difference. It will mean that it actually works, instead of causing memory leaks and all sorts of other nasties like you are having.

These are not merely suggestions, like telling you that you could replace a division with a shift or something trivial and unimportant like that. These are vital corrections. You will not get a correctly functioning program without doing so, and you will not fix your memory leaks.

Failing to match new and delete, or new[] and delete[], is precisely what is probably causing the leaks to begin with.

If you don't understand the advice, then ask more questions, or post code so that we can see what you are doing wrong.