I have this C++ code:
#include
using namespace std;
struct MyItem
{
int value;
MyItem* nextItem;
};
int main() {
MyItem item = new
You've mixed
MyItem item;
which allocates an instance of MyItem on the stack. The memory for the instance is automatically freed at the end of the enclosing scope
and
MyItem * item = new MyItem;
which allocates an instance of MyItem on the heap. You would refer to this instance using a pointer and would be required to explicitly free the memory when finished using delete item.