Suppose I have a struct called Node as follows:
struct foo
{
foo *next;
int aNum;
};
How do I create an “instance” of this in C++ (in a main method for instance)? I’ve looked around and it seems as if I would just do
foo name;
But it seems to me as if I should have to allocate space first. Can someone explain (a long explanation is not necessary)
If you define your instance like this, it is an object of automatic storage duration. That means that the compiler generates code for you that takes care of allocating and freeing the memory for that object.
This method has limitations. The lifetime of an object created in this way is always bound to the lifetime of a surrounding function or an owning object. Also, if the object is a local variable inside a function, it will usually be allocated on the stack, which is usually quite limited in size.