MSVC2017 static runtime heap - visual-c++

I have A.dll and B.dll that links MSVC2017 runtime statically. Do they have separate heaps or share the same heap when they are loaded to the same exe module?
Can I pass std::string with the default allocator, for example, from A.dll to B.dll by value?

Each has their own separate heap manager. I expect passing std::string by value to end badly.
In general, since C++ doesn't define ABI, it's unwise to use C++ classes in general, and standard library classes in particular, in a DLL's public interface. One exception is when a) all modules (EXE and DLLs) link to the DLL runtime, and b) they are all built together, using the same version of the same compiler (at which point, there's little benefit in splitting into multiple modules in the first place).
There are two common approaches to designing DLL interface:
C-style free functions using only fundamental types, and structs and arrays thereof. Windows API is mostly like that.
Pointers to abstract classes with no data members and all methods pure virtual - also known as interfaces. At this point, you are using COM, or something substantially COM-like.
In either case, the program must arrange that all resources allocated by a DLL are deallocated in the same DLL. E.g. the DLL may require the caller to pass in buffers for the DLL to fill, so it doesn't return allocated memory; or provide a function the caller must use to deallocate memory allocated by the DLL; or use OS facilities such as CoTaskMemAlloc et al, and document this use.

Related

How to correctly use std::arch::_mm_loadu_si128 / _mm_storeu_si128

Usually one should be wary of transmuting (or casting) pointers to a higher alignment. Yet the interface to the above functions require *const _m128i and *mut _m128i pointers, respectively. Both are SIMD-aligned, which means I'd need to keep my arrays SIMD-aligned, too. On the other hand, the intrinsics are explicitly designed to load/store unaligned data.
Is this safe? Shouldn't we change the interface? Or at least document this fact?
I think this is a cross-language duplicate of Is `reinterpret_cast`ing between hardware vector pointer and the corresponding type an undefined behavior?.
As I explained over there, Intel defined the C/C++ intrinsics API such that loadu / storeu can safely dereference an under-aligned pointer, and that it's safe to create such pointers, even though it's UB in ISO C++ even to create under-aligned pointers. (Thus implementations that provide the intrinsics API must define the behaviour).
The Rust version should work identically. Implementations that provide it must make it safe to create under-aligned __m128i* pointers, as long as you don't dereference them "manually".
The other API-design option would be to have another version of the type that doesn't imply 16-byte alignment, like a __m128i_u or something. GNU C does this with their native vector syntax, but that's way off topic for Rust.

vftable - what is this?

What is vftable in high programming languages?
I read something like it's the address of a virtual object structure, but this is a pretty messy information
Can someone please explain it?
It most likely stands for "Virtual Function Table", and is a mechanism used by some runtime implementations in order to allow virtual function dispatch.
Mainstream C++ implementations (GCC, Clang, MSVS) call it the vtable. C has no polymorphism. I could only speculate about other languages.
Here's what Wikipedia says on the topic:
An object's dispatch table will contain the addresses of the object's
dynamically bound methods. Method calls are performed by fetching the
method's address from the object's dispatch table. The dispatch table
is the same for all objects belonging to the same class, and is
therefore typically shared between them. Objects belonging to
type-compatible classes (for example siblings in an inheritance
hierarchy) will have dispatch tables with the same layout: the address
of a given method will appear at the same offset for all
type-compatible classes. Thus, fetching the method's address from a
given dispatch table offset will get the method corresponding to the
object's actual class.[1]
The C++ standards do not mandate exactly how dynamic dispatch must be
implemented, but compilers generally use minor variations on the same
basic model.
Typically, the compiler creates a separate vtable for each class. When
an object is created, a pointer to this vtable, called the virtual
table pointer, vpointer or VPTR, is added as a hidden member of this
object (becoming its first member unless it's made the last[2]). The
compiler also generates "hidden" code in the constructor of each class
to initialize the vpointers of its objects to the address of the
corresponding vtable. Note that the location of the vpointer in the
object instance is not standard among all compilers, and relying on
the position may result in unportable code. For example, g++
previously placed the vpointer at the end of the object.[3]
Ellis & Stroustrup 1990, pp. 227–232
Heading "Multiple Inheritance"
CodeSourcery C++ ABI
Vftable is not explicitly mentioned in the C++ standard, but most (if not all) implementations use it for virtual function implementation.
For each class with virtual functions the compiler creates an array of function poiners which are the pointers to the last overriden version of the virtual functions of that class. Then each object has a pointer to the vtable of its dynamic class.
See this question and its accepted answer for more illustrations
Virtual dispatch implementation details

Is it safe to return a struct from an stdcall dll function?

I am designing an API that has to be binary-compatible between at least mingw and msvc++. So far I have restricted myself to using function which take and return primitive data types or pointers to POD-structs with uniform members (i.e. the members are all of the same type, which should reduce the risk of incompatible padding).
It would be convenient at some points to return structs by value though, so that the callee does not need to keep a temporary copy. So the question is: Is it safe to pass structs by value to/from stdcall functions, when the callee was compiled by a different compiler than the caller? Does this still hold for less recent versions of msvc and mingw? I would be more confident that it is, but I found this topic discussing a problem in this exact situation with cdecl calling convention, which was apparently only solved in mingw 4.6.
Using struct just like is not good option. You need to use
#pragma pack
Refer http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Fcompiler%2Fref%2Frnpgpack.htm
http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx
And make sure mingw respect pragma instruction.
I don't know mingw, but if it can call Win32 APIs then it can pass structs in a way that is compatible with stdcall - since many Win32 APIs are both stdcall and take structs.
Martyn

statically/dynamically typed vs static/dynamic binding

everyone what is the difference between those 4 terms, can You give please examples?
Static and dynamic are jargon words that refer to the point in time at which some programming element is resolved. Static indicates that resolution takes place at the time a program is constructed. Dynamic indicates that resolution takes place at the time a program is run.
Static and Dynamic Typing
Typing refers to changes in program structure that are due to the differences between data values: integers, characters, floating point numbers, strings, objects and so on. These differences can have many effects, for example:
memory layout (e.g. 4 bytes for an int, 8 bytes for a double, more for an object)
instructions executed (e.g. primitive operations to add small integers, library calls to add large ones)
program flow (simple subroutine calling conventions versus hash-dispatch for multi-methods)
Static typing means that the executable form of a program generated at build time will vary depending upon the types of data values found in the program. Dynamic typing means that the generated code will always be the same, irrespective of type -- any differences in execution will be determined at run-time.
Note that few real systems are either purely one or the other, it is just a question of which is the preferred strategy.
Static and Dynamic Binding
Binding refers to the association of names in program text to the storage locations to which they refer. In static binding, this association is predetermined at build time. With dynamic binding, this association is not determined until run-time.
Truly static binding is almost extinct. Earlier assemblers and FORTRAN, for example, would completely precompute the exact memory location of all variables and subroutine locations. This situation did not last long, with the introduction of stack and heap allocation for variables and dynamically-loaded libraries for subroutines.
So one must take some liberty with the definitions. It is the spirit of the concept that counts here: statically bound programs precompute as much as possible about storage layout as is practical in a modern virtual memory, garbage collected, separately compiled application. Dynamically bound programs wait as late as possible.
An example might help. If I attempt to invoke a method MyClass.foo(), a static-binding system will verify at build time that there is a class called MyClass and that class has a method called foo. A dynamic-binding system will wait until run-time to see whether either exists.
Contrasts
The main strength of static strategies is that the program translator is much more aware of the programmer's intent. This makes it easier to:
catch many common errors early, during the build phase
build refactoring tools
incur a significant amount of the computational cost required to determine the executable form of the program only once, at build time
The main strength of dynamic strategies is that they are much easier to implement, meaning that:
a working dynamic environment can be created at a fraction of the cost of a static one
it is easier to add language features that might be very challenging to check statically
it is easier to handle situations that require self-modifying code
Typing - refers to variable tyes and if variables are allowed to change type during program execution
http://en.wikipedia.org/wiki/Type_system#Type_checking
Binding - this, as you can read below can refer to variable binding, or library binding
http://en.wikipedia.org/wiki/Binding_%28computer_science%29#Language_or_Name_binding

How to get calling convension of a function from type library?

How can I get the calling convention at runtime using type library whether using stdcall, cdecl, winapi or any other?
If a typelibrary is involved, you're using COM. COM always uses the same calling convention (on a given OS), so it isn't encoded into the type library. In the headers it shows up as STDMETHOD (or something like that -- I'm too lazy to check the exact spelling at the moment), but if memory serves it's basically equivalent to __cdecl. If memory serves, there's also an _STDMETHOD (or, again, something similar) that lets you specify a return type other than HRESULT.

Resources