I've got two modules. One calls other one and passes as an argument a structure.
struct {
char* szDGRTag;
bool bTagEx;
} ADPTAG;
Master is written in C++ from '98 in Visual C++ 6.0. Slave is written in C++ 11 in Visual Studio 2010 Professional.
Function that is called in Slave:
long lCheckPresenceOfFields (char* szName, ADPTAG* AdpTagList, long lNbVar)
In Master:
long lNbVar = 2;
ADPTAG* AdpTagList = NULL;
AdpTagList = new ADPTAG[lNbVar];
AdpTagList[0].szTag = new char[32];
AdpTagList[0].bTagEx = true;
memset(AdpTagList[0].szTag, 0x0, 32+1);
AdpTagList[0].szTag = NULL;
AdpTagList[1].szTag = new char[32];
AdpTagList[1].bTagEx = true;
memset(AdpTagList[1].szTag, 0x0, 32+1);
AdpTagList[1].szTag = NULL;
int size = sizeof(AdpTagList[0]);
AdpTagList[0].szTag = "DDD";
AdpTagList[1].szTag = "AAA";
long pres = pGetFieldsPresence(szPath, AdpTagList, 2);
I've checked size of AdpTagList[0] in Master and it was 5 bytes, but in Slave it is 8 bytes, so the first object is OK, but any next is bad, because pointer points to wrong memory area.
What can be the issue here? Different compilator? Probably not as structure with only char* and int works fine between these two modules.
Regardlesss of size of 1st object [0], second [1] is null-pointer.
You're running into a packing discrepancy. You can modify the packing with the "/Zp" compiler option.
Since by default Visual Studio:
Packs structures on 8-byte boundaries
It's very likely that your Visual Studio 2010 compiler does not specify packing. Your Visual C++ 6.0 application likely did specify 1-byte packing as a structure containing an int
and a char
should pack into that.
Using 1-byte packing optimizes a programs system memory footprint, because no memory is lost to padding.
Using 8-byte packing optimizes speed because it increases the chance that structures can be fetched in a single load.
For more on packing you can read here: https://msdn.microsoft.com/en-us/library/71kf49f1.aspx
But I'd go with Microsoft's statement:
You should not use this option unless you have specific alignment requirements
Meaning since you have to change one, I'd recommend changing the packing on the Visual C++ 6.0 application.