How does C++ store in memory instances of classes and structs and if they are difference in terms of performance? [duplicate]

StackOverflow https://stackoverflow.com/questions/21325736

Domanda

I want to ask how C++ stores in the memory the class and struct instantiations and if there is any difference in terms of performance. I know that C# stores the class objects in the heap and the struct objects on the stack, so struct object instances are a bit faster than class object instances. Is it similar in C++?

È stato utile?

Soluzione

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.

Altri suggerimenti

There is no difference from a memory perspective. Only difference is that structs default to public members where classes default to private. This is for C compatibility.

Stack/heap decisions are made per object rather than per class/struct by using pointers along with the new keyword.

Actually, in C++ if you are creating a class instance using new or malloc then memory will be assigned in Heap.

Ex.

ClassName * objName = new ClassName();

and when you use class object as a variable in side a function as follows

void main()
{
ClassName objName;
}

Then at this time the memory will be allocated in Stack segment.

Here is also one good article about Dynamic Allocation in c++

Hope this will help you.

I know that C# stores the class objects in the heap and the struct objects on the stack

No it doesn't. C# classes are reference types while its structs are value types, but that alone doesn't imply the behaviour you describe. See MSDN: 'value types are allocated either on the stack or inline in containing types and deallocated when the stack unwinds or when their containing type gets deallocated.'

so struct object instances are a bit faster than class object instances.

That doesn't follow, and it also isn't true.

Is it similar in C++?

It doesn't work like that in C++ either. Automatic objects go on the stack; dynamic objects go in the heap; and whether 'struct' or 'class' has nothing to do with it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top