IT-Consultant: Frederick J. Harris > PowerBASIC Programmer's Survival Guide To C And C++

Difference between a PB Type and a C++ struct?

(1/6) > >>

Heinz Grandjean:
On my way into the "C++ heaven" (oh god) I ran into a problem:
I tried to send a PB Type from a PB-program as an argument to an export-function inside a  C++ DLL. 
So far I got it working, but access to the members inside the C++ Dll was always  postponed.
I could overcome this by doing the following:

Normal practice in PB:

--- Code: ---TYPE MyNewType
     Var1  AS LONG
     Var2  AS WORD
     Var3  AS BYTE
END TYPE 

--- End code ---

Adding a dummy-member in C++

--- Code: ---struct MyNewStruct
{
signed int     dummy; //added !!!!
signed int     Var1;
        short int      Var2;
unsigned _int8 Var3;
};
--- End code ---

This solves the problem for the moment, but it is not a correct solution???

Thanks for help,
Heinz Grandjean
 

Patrice Terrier:
What about this


--- Code: ---struct MyNewType {
    LONG var1;
    WORD var2;
    BYTE var3;
};
--- End code ---

Kev Peel:
You need to set the structure packing value. PowerBASIC uses 1-byte packing by default and the C dialects can vary (4 bytes for SDK and MS VC++).
 
If using Microsoft VC++, use the following code:
 

--- Code: ---#pragma pack (push)
#pragma pack(1)

// Structure defines

#pragma pack (pop)
--- End code ---

That will force it to be identical to PB's TYPE/UNIONs

Frederick J. Harris:
Because of this, it has become my practice to attempt, when I create my PowerBASIC UDTs, to create them in multiples of 8 or 16 bytes.   To achieve this I'll often put dummy fillers at the end, just as shown above.

Note that a type such as this will work without problems in both C and PowerBASIC ...


--- Code: ---struct MyStruct
{
  short int iSp;    // 2 bytes
  short int iDbh;  // 2 bytes
  short int iHt;     // 2 bytes
  short int iCull;  // 2 bytes
};

--- End code ---



--- Code: ---Type Tree
  iSp As Integer
  iDbh As Integer
  iHt As Integer
  iCull As Integer
End Type

--- End code ---

... because both are 8 bytes in size, which is some sort of memory granulation boundary.

I believe there are some obscure compiler command line switches in C/C++ that can control this, but I've personally never fooled with them.

To be perfectly honest, padding structs or types with extra dummy variables can become useful for when application requirements change and extra variables are needed.  Especially when the UDTs are used for templates of file storage into records.

Frederick J. Harris:
Also, if you use C's sizeof operator on a struct which, for example, contains a four byte int, a 2 byte word, and a one byte char, the sizeof operator will return 8 bytes - not 7.  I'm going to check that out quick .../ hold on ...

Yep.


--- Code: ---#include <cstdio>

struct MyStruct
{
 int       iInteger;        // 4
 short int iShortInt;   //  2
 char      c;                 // 1
};

int main()
{
 printf("sizeof(MyStruct) = %d\n",sizeof(MyStruct));
 getchar();

 return 0;
}

//sizeof(MyStruct) = 8

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version