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

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

<< < (2/6) > >>

José Roca:
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.

Heinz Grandjean:
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: ---#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                       

--- End code ---

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

Definition of struct:

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

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

--- End code ---

declare:

--- Code: ---extern "C"
{
      __declspec(dllexport) long Testfunction(MyNewStruct);
}

--- End code ---

cpp:

--- Code: ---//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;
}


--- End code ---

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

Thanks again
Heinz Grandjean

Kev Peel:
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..

Charles Pegge:
How does C++ handle arrays of structs?

For instance the 24 bit RGB pixels in a BMP.

Frederick J. Harris:

--- Quote ---How does C++ handle arrays of structs?

--- End quote ---

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. 

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version