Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 09 Dec 1999
Offline Last Active Private

Posts I've Made

In Topic: Git Visual Studio *.vcxproj.filters file merge problem

26 June 2016 - 04:54 PM

Short answer: don't bother versioning the filters file. Those are the bits of the project file that got pulled out because they were related to non-code IDE state.

In Topic: Why is my struct destructor not called?

07 April 2016 - 12:19 AM

You could explicitly invoke the dtor before you invoke the ctor again.
a = A();

This would be a bad idea in the general case. The assignment operator would assume that the object is in a valid state, but the destructor for most non trivial objects would put it in an invalid state. Ex: it may deallocate internal buffers, but the assignment operator may assume that those buffers are allocated.

In Topic: Can memory leaks only caused by using the new keyword?

26 March 2016 - 05:47 PM

Memory can also be directly allocated with malloc() or related functions such as realloc() or calloc() or with direct OS calls such as the HeapAlloc() function in Windows.

In Topic: C++ interfaces and library decoupling

24 January 2016 - 07:34 PM

Because of this, code compiled with C++ compiler A cannot talk to code compiled with C++ compiler B. And compiler vendor A could change their mind between versions, changing things again, so you can't even use code compiled with different compilers from the same vendor.

This is an over-generalization. Again, COM interfaces are quite frequently implemented as C++ classes and work quite well across compilers on Windows. You can get a pointer to a COM object implemented as a MSVC C++ class and directly invoke a member function on it from BCB compiled C++ program and vice versa. You can't freely inter-operate between compilers (in fact, interface classes whose arguments are only primitive and pointer types is about the only C++ feature that works reliably), but that's different from saying it can't work at all.

In comparison, C defines a standard binary layout for its structs, arrays, and the like, as well as things like not mangling function names. Because of this, C code compiled with any compiler, can talk to C code compiled by any other compiler.

Actually, it doesn't. In particular, primitives don't have set sizes (only minimum sizes) and structs may have vendor specific padding between members. It's also entirely possible for one compiler for a given platform to have primitive types with sizes unavailable to some other compilers on the same platform (historically common with 64-bit and 128-bit types). There's also the fun realm of incompatible vendor specific calling conventions (ex: Microsoft C, Turbo C and Watcom C all defined a __fastcall convention but each used a different number of registers). For that matter, some vendors just never bothered implementing broad parts of C99, so any features from that aren't guaranteed to be portable. Usually the only ones that cause problems for library interfaces are flexible array members and the _Bool type.

Functions must be defined under C-type calling convention, cannot have any overrides, and cannot be members of structs. Classes cannot be exposed.

Actually, on x86 Windows most cross-compiler code seems to use the Pascal calling convention (arguments passed left to right with callee cleanup) rather than C calling conventions (arguments passed right to left with caller cleanup). In particular, almost every Win32 API function uses the Pascal calling convention. And again, in COM, C++ interface classes are frequently exposed.

In Topic: C++ interfaces and library decoupling

23 January 2016 - 07:51 PM

To be fair, each library still has to know about the other in COM because they have to have access to the interface definition.

Not necessarily. It's a lot easier if the static types of all the interfaces necessary are available, but a library that implements the IDispatch interface can be dynamically interrogated and manipulated. Both libraries need to know about COM automation, but they don't necessarily need to know about each other. For example, this is how scripting support for new components was implemented in Office (up to around 2003, no idea if later versions of Office changed their inner workings). If you added a new charting widget to your worksheet, Excel wouldn't know about new interfaces it added over the base OLE stuff, but you could still manipulate them through VBA because the charting widget would implement the IDispatch interfaces VBA would use to make the actual calls.

For dynamic libraries your options are far more limited, as C++ cannot be used. (Insert arguments about how C++ can be used under special conditions A, B, or C) In most cases you'll be dealing with C APIs and things like function pointers.

You may want to be a lot more clear about what you mean by "C++ cannot be used" as it's pretty obvious that people do write dynamic libraries in C++. Again, many COM DLLs are written in C++. Right now your statement only makes sense if you already know what it means.