Deprecated: Array and string offset access syntax with curly braces is deprecated in /homepages/21/d38531796/htdocs/jose/smfforum/Sources/Subs.php on line 3825
Difference between a PB Type and a C++ struct?

Author Topic: Difference between a PB Type and a C++ struct?  (Read 16452 times)

0 Members and 1 Guest are viewing this topic.

Offline Heinz Grandjean

  • Newbie
  • *
  • Posts: 18
  • User-Rate: +0/-0
Difference between a PB Type and a C++ struct?
« on: September 19, 2013, 11:27:37 AM »
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: [Select]
TYPE MyNewType
     Var1  AS LONG
     Var2  AS WORD
     Var3  AS BYTE
END TYPE 

Adding a dummy-member in C++
Code: [Select]
struct MyNewStruct
{
signed int     dummy; //added !!!!
signed int     Var1;
        short int      Var2;
unsigned _int8 Var3;
};

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

Thanks for help,
Heinz Grandjean
 

Offline Patrice Terrier

  • ROMs
  • Hero Member
  • *****
  • Posts: 934
  • User-Rate: +62/-1
    • www.zapsolution.com
Re: Difference between a PB Type and a C++ struct?
« Reply #1 on: September 19, 2013, 02:20:08 PM »
What about this

Code: [Select]
struct MyNewType {
    LONG var1;
    WORD var2;
    BYTE var3;
};
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Kev Peel

  • Newbie
  • *
  • Posts: 7
  • User-Rate: +1/-0
Re: Difference between a PB Type and a C++ struct?
« Reply #2 on: September 19, 2013, 02:45:18 PM »
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: [Select]
#pragma pack (push)
#pragma pack(1)

// Structure defines

#pragma pack (pop)

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

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Difference between a PB Type and a C++ struct?
« Reply #3 on: September 19, 2013, 03:07:45 PM »
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: [Select]
struct MyStruct
{
  short int iSp;    // 2 bytes
  short int iDbh;  // 2 bytes
  short int iHt;     // 2 bytes
  short int iCull;  // 2 bytes
};


Code: [Select]
Type Tree
  iSp As Integer
  iDbh As Integer
  iHt As Integer
  iCull As Integer
End Type

... 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.

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Difference between a PB Type and a C++ struct?
« Reply #4 on: September 19, 2013, 03:23:33 PM »
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: [Select]
#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
« Last Edit: September 19, 2013, 03:30:27 PM by Frederick J. Harris »

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • User-Rate: +204/-0
Re: Difference between a PB Type and a C++ struct?
« Reply #5 on: September 19, 2013, 03:32:41 PM »
PB did choose the VB path regarding padding. This is inconvenient when working with C++ libraries. Many of the TYPEs in the official PB includes are misaligned. I checked all the ones in my headers with the size given by Visual C and added filler members to keep the correct alignment. The problem is becoming more noticeable since the coming of 64-bit. With 32-bit, the norm was to use 4 bytes alignment, but many of the new APIs use QUADs and 8 bytes alignment.

Offline Heinz Grandjean

  • Newbie
  • *
  • Posts: 18
  • User-Rate: +0/-0
Re: Difference between a PB Type and a C++ struct?
« Reply #6 on: September 19, 2013, 04:03:45 PM »
Thank you for the replies.

Like Frederick Harris I tested the sizeof in C++.
When I use Kev Peels advice: #pragma pack(1) , I get 7!
Without that I get 8, like Frederick Harris has demonstrated.
But there is no progress to the problem, sorry.
When I want to see Var1 (set in PB to 15, I get a value of  1637928).
This seems to me like a sort of internal header or a pointer or anything in that direction.
When I want to see Var2 then I get Var1 and so on.

The complete PB test routine:
Code: [Select]
#COMPILE EXE
#DIM ALL

TYPE MyNewType
     Var1  AS LONG
     Var2  AS WORD
     Var3  AS BYTE
END TYPE

DECLARE FUNCTION myTestFunction CDECL LIB "D:\ESTWGJ_Quell\C++\Betrieb_DLL\Debug\Betrieb_Dll.dll" ALIAS "Testfunction" (MyNewType)  AS LONG

FUNCTION PBMAIN () AS LONG
'**************************

 LOCAL MyNewT AS MyNewType
 MyNewT.Var1 = 15
 MyNewT.Var2 = 19
 MyNewT.Var3 = 255

 ?STR$( myTestFunction(MyNewT))

END FUNCTION                       

The Counterpart in C++8; I use MS Visual Studio Express 2010

Definition of struct:
Code: [Select]
#pragma pack (push)
#pragma pack(1)

struct MyNewStruct
{
    signed int     Var1;
    short int      Var2;
    unsigned _int8 Var3;
};
#pragma pack (pop)

declare:
Code: [Select]
extern "C"
{
      __declspec(dllexport) long Testfunction(MyNewStruct);
}

cpp:
Code: [Select]
//Betrieb_DLL_Main.cpp

#include <stdio.h>
#include <windows.h>
#include "D:\ESTWGJ_Quell\C++\Header\Dat_Datenheader.h"
#include "Betrieb_Header.h"

long Testfunction (MyNewStruct newT)
{
return newT.Var1;
}


By the way, the C++ compiler doesn't allow "word or byte".

Thanks again
Heinz Grandjean

Offline Kev Peel

  • Newbie
  • *
  • Posts: 7
  • User-Rate: +1/-0
Re: Difference between a PB Type and a C++ struct?
« Reply #7 on: September 19, 2013, 05:17:30 PM »
It looks correct to me the correct size is 7 (4+2+1).

IMO the full code was not provided so I am thinking the struct is not being initialized to zero and you are seeing a random value. Try memset() after declaring the struct..

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 699
  • User-Rate: +27/-1
    • Charles Pegge
Re: Difference between a PB Type and a C++ struct?
« Reply #8 on: September 19, 2013, 06:25:40 PM »
How does C++ handle arrays of structs?

For instance the 24 bit RGB pixels in a BMP.

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Difference between a PB Type and a C++ struct?
« Reply #9 on: September 19, 2013, 06:58:06 PM »
Quote
How does C++ handle arrays of structs?

Just off the top of my head without testing a specific instance, my guess Charles would be that an odd sized struct, e.g., the seven byte one or your three byte example, would get bumped up to the nearest 4 byte boundary.  And the offset of each instance of the struct in memory would be sizeof(struct) bytes from the initial allocation address.  So I'm guessing if you had a three byte struct, i.e., three chars, each struct instance would still occupy four bytes.  Should be easy to test, but that's where I'd put my money. 

Offline Heinz Grandjean

  • Newbie
  • *
  • Posts: 18
  • User-Rate: +0/-0
Re: Difference between a PB Type and a C++ struct?
« Reply #10 on: September 19, 2013, 07:01:09 PM »
Well, I maybe found it.
You have to use pointers in the C++ DLL.

Code: [Select]
__declspec(dllexport) long Testfunction(MyNewStruct*);

long Testfunction (MyNewStruct* newT)
{
MyNewStruct newT1 = *newT;
return newT1.Var2;
}

I don't know this to be the ultimate solution, but now it does the job!
I even can remove the #pragma pack...
Thank you for your participation.
Heinz Grandjean

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Difference between a PB Type and a C++ struct?
« Reply #11 on: September 19, 2013, 07:25:58 PM »
Well, if this is your PowerBASIC declare ...

Code: [Select]
DECLARE FUNCTION myTestFunction CDECL LIB "D:\ESTWGJ_Quell\C++\Betrieb_DLL\Debug\Betrieb_Dll.dll" ALIAS "Testfunction" (MyNewType)  AS LONG

... then you are passing the address of an instance of MyNewType. 
« Last Edit: September 19, 2013, 07:59:19 PM by Frederick J. Harris »

Offline Heinz Grandjean

  • Newbie
  • *
  • Posts: 18
  • User-Rate: +0/-0
Re: Difference between a PB Type and a C++ struct?
« Reply #12 on: September 19, 2013, 07:36:00 PM »
It also works with:
Code: [Select]
DECLARE FUNCTION myTestFunction CDECL LIB "D:\ESTWGJ_Quell\C++\Betrieb_DLL\Debug\Betrieb_Dll.dll" ALIAS "Testfunction" (BYVAL MyNewType PTR)  AS LONG               
No difference as far as I can oversee it.
Heinz Grandjean

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Difference between a PB Type and a C++ struct?
« Reply #13 on: September 19, 2013, 08:23:09 PM »
Here's my attempt to do it ...

In C++

Code: [Select]
#include <cstdio>

struct MyNewType
{
 int var1;
 short var2;
 unsigned char var3;
};

extern "C" int __declspec(dllexport) MyTestFunction(MyNewType* mnt)
{
 printf("mnt->var1=%d\n",mnt->var1);
 printf("mnt->var2=%d\n",mnt->var2);
 printf("mnt->var3=%d\n",mnt->var3);

 return 1;
}

And PB CC Client ...

Code: [Select]
#Compile Exe
#Dim All

Type MyNewType
  Var1  AS LONG
  Var2  AS WORD
  Var3  AS BYTE
End Type

DECLARE FUNCTION MyTestFunction CDECL LIB "Heinz1.dll" ALIAS "MyTestFunction" (MyNewType)  AS LONG

Function PBMain() As Long
  Local MyType As MyNewType

  MyType.var1=15
  MyType.var2=19
  MyType.var3=255
  Call MyTestFunction(MyType)
  Waitkey$

  PBMain=0
End Function

' Output
' ===========
' mnt->var1=15
' mnt->var2=19
' mnt->var3=255
                   

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: Difference between a PB Type and a C++ struct?
« Reply #14 on: September 19, 2013, 09:25:40 PM »
So what is going on here in your last example Heinz is that within TestFunction the passed in pointer to a MyNewStruct is being used to initialize a new 2nd local instance of a MyNewStruct with the one passed in through the parameter...

Code: [Select]
__declspec(dllexport) long Testfunction(MyNewStruct*);

long Testfunction (MyNewStruct* newT)
{
MyNewStruct newT1 = *newT;  // initialize newT1 with what is stored at newT, i.e., *newT
return newT1.Var2;
}

And being that newT1 is a local instance rather than a pointer to a local instance, one can use the dot operator to return the MyNewStruct::Var2 member.

However, by using the '->' pointer notation as seen in my example above, you can directly reference the MyNewStruct::Var2 member, i.e.,

return newT->Var2;
« Last Edit: September 19, 2013, 09:28:42 PM by Frederick J. Harris »