when do I need Align #PB_Structure_AlignC

Just starting out? Need help? Post your questions and find answers here.
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

when do I need Align #PB_Structure_AlignC

Post by Wolfram »

Can someone explain me when do I need Align #PB_Structure_AlignC?
Is it always if I build a structure based on C code or just in special cases?
macOS Catalina 10.15.7
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: when do I need Align #PB_Structure_AlignC

Post by skywalk »

My simplest answer is:
Run sizeof(myCStruct) in C/C++.
Run sizeof(myPBStruct) in PB.
The 2 byte counts must match, else you need to insert padding and/or use #PB_STRUCTURE_ALIGNC.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: when do I need Align #PB_Structure_AlignC

Post by Wolfram »

Thanks, thats the way I work, but it would be more easy if I understand the logic behind.
macOS Catalina 10.15.7
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: when do I need Align #PB_Structure_AlignC

Post by spikey »

It depends if you will be interacting with code which will expect the C convention to be used:-

If you are supplying structured parameters to OS API functions or functions in a third party library written in C and the supplied data is not in the correct alignment the parameters will be erroneously interpreted by the receiving function call and either an error will be raised, if one is detectable, or the function call will malfunction in some way which may not be immediately obvious or detectable.

If you are writing a library which might or will be used by programs using the C convention, then you will need to conform.

If you are converting code from C which will henceforth be used only in PureBasic, you could conceivably ignore it, providing none of the above is true.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: when do I need Align #PB_Structure_AlignC

Post by Josh »

Be careful when using SizeOf() in C++. You have to import the appropriate header file and not make a SizeOf() on a structure you have copied to your C++ code. In the second case, the structure is always aligned, which is not the case.
sorry for my bad english
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: when do I need Align #PB_Structure_AlignC

Post by Wolfram »

spikey wrote:
If you are supplying structured parameters to OS API functions or functions in a third party library written in C and the supplied data is not in the correct alignment the parameters will be erroneously interpreted by the receiving function call and either an error will be raised, if one is detectable, or the function call will malfunction in some way which may not be immediately obvious or detectable.
if I understand you correctly, I always have to use #PB_Structure_AlignC when I work with the API of the OS.
I've done alot without #PB_Structure_AlignC, but that was lucky it worked.
macOS Catalina 10.15.7
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: when do I need Align #PB_Structure_AlignC

Post by Josh »

Wolfram wrote:if I understand you correctly, I always have to use #PB_Structure_AlignC when I work with the API of the OS.
No, don't do that. You really have to check each structure individually, whether it needs to be aligned or not.
sorry for my bad english
Post Reply