Search code examples
c++classloader

Are cpp classes dynamically loaded?


I know that the JVM has a separate class loader which is the default runtime method for loading classes and executing them. I just want to know how class loading works for cpp. And is class loading in cpp somewhat related to the dynamic loading in operating systems. If the classes are not dynamically loaded I guess there is no real need for heap allocation. But cpp obviously has the free() memory option. I'm slightly confused. How does it work exactly?

EDIT: I understood why the heap is required for object initialization. But the reasons for static or dynamic loading of classes itself is not clear to me, especially the way cpp does it.


Solution

  • No.
    Once the code is compiled into an executable there are no classes, objects, methods and such: only bytes and machine instructions.

    Dynamic memory has nothing to do with dynamic class loading. "Dynamic" memory implies that the size of the memory is not necessarily known at compile time and might only be known at runtime, with the ability to change and be resized, hence the "dynamic". Again, this is achieved using machine instructions which have no knowledge of classes.

    The only thing that remotely resembles "class loading" is DLL loading. A DLL is compiled code which exports functions for other programs to use.
    The DLL can be loaded and unloaded at runtime. And again, these exported functions are already machine instructions.