In C++, the only difference the keywords struct
and class
make is whether the members are public or private by default, and what the default access specifier for a base class (whether the base class is public or private) is if you do not specify one. Otherwise, instances of structs or classes are identical.
I don't believe the standard dictates the working of memory, and whether an instance is on a "heap" or a "stack" (or whether such things exist); this is up to the implementation. That said, on most systems, an "automatic" variable, i.e., one declared normally like:
void my_function() {
MyClassType my_variable;
}
… is typically allocated on the stack. Objects allocated with new
are typically allocated on the heap.
If there is a performance difference, it is typically of minor concern. new
is typically used when dynamic allocation is required by the structure of the program; automatic (stack) allocation otherwise, as it's easier to ensure cleanup. I.e., the structure and requirements of the program typically dictate whether or not new
(or something fancier, like smart pointers) is used.