2

I couldn't come up with a more proper name for the question, but I think it'll be clear with the examples below.

What is the fundamental difference (memory-wise) between:

  1. A DLL that contains the following code:

    class ISDLConsole { /* interface */ };
    class SDLConsole: public ISDLConsole { /* implementation */ };
    __declspec(dllexport) ISDLConsole *Create()
    {
        return new SDLConsole();
    }
    

    with a client that has linked dynamically with this DLL and called:

    ISDLConsole * pClientSDLConsole = Create();
    
  2. A client of boost, that links with it dynamically, and uses one of their containers as follows:

    boost::numeric::ublas::vector<double> v(1000);

Now, if I didn't already assume something wrong, it seems to me that in both cases there is a client that is linked with a DLL, invokes a method (in the boost case it's the vector::vector() c'tor) which allocates dynamic memory.

I assume that it would be better to replace ISDLConsole * pClientSDLConsole = Create(); with ISDLConsole * pClientSDLConsole = new SDLConsole(); (export the class itself if possible) and allocate memory only on the client side, right?

So:

  • What is the fundamental difference (memory-wise) between 1 and 2? are both cases the same in terms of memory-accorss-dlls? if so, please clarify how.
  • If they're not the same, why would someone recommend allocating a new object across DLL and not do it the 'boost' way I mentioned above (if possible).
  • If I want to write my own container and put it in a separate DLL, what parts of it should be '__declspec(dllexport)ed'? and does it mean there will be memory moving across DLLs (assuming my container allocates dynamic memory)?

Any clarifications on the subject are more than welcome. Thank you very much.

The source for the ISDLConsole example is this accepted answer, which BTW contradicts this accepted answer.

Community
  • 1
  • 1
Asaf
  • 55
  • 5
  • You are badly mixing things up. std::vector is a template class from the C++ library. Not a DLL at all. Boost is not a DLL either, they avoid accidents by very carefully naming their libraries. As long as you don't have a good feel for where code lives you'd be wise to *not* cut corners. – Hans Passant Feb 15 '15 at 16:34
  • @HansPassant , I didn't mean std::vector, but boost's vector, my bad for not specifying the full namespace (updated the question). And there are few boost DLLs out there for sure. I'm not sure if boost containers which I mentioned in my question can be dynamically linked with though.. – Asaf Feb 16 '15 at 13:43

2 Answers2

1

In terms of heap allocation, I see no difference. In both cases, you get a class instance allocated on the heap. An instantiated object in the heap should take up the same amount of memory whether or not it was instantiated in them main executable, or a library.

In terms of code, I would expect to be a few bytes' worth of difference, between the two approaches. Background noise, nothing I would be concerned with.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0
  1. Code pClientSDLConsole = new ISDLConsole(); will not compile because ISDLConsole is an interface (abstract class).
  2. Code pClientSDLConsole = new SDLConsole(); will compile only if DLL developer declared SDLConsole in public header and exported it. But it contraticts the principle of hiding the implementation.
  3. boost::vector approach is fundamentally different: all vector code in inline, there is nothing to export. Some boost libraries do not follow this rule, e.g. boost::filesystem.
Nikerboker
  • 764
  • 7
  • 14
  • Regarding 1 - thanks for pointing that out. I updated the question. If I understood correctly, aren't you implying in that boost::vector also contradicts the principle of hiding the implementation when they implement everything inline in the header file? And does it make sense to link dynamically with such implementations? Thanks for your comment. – Asaf Feb 16 '15 at 13:51
  • Hiding the implementation is useful if: (1) you want to maintain binary compatibility, (2) you do not want to distribute source code. Stl and Boost have another goals. It's up to you to select the appropriate principle. – Nikerboker Feb 16 '15 at 18:42