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?
when do I need Align #PB_Structure_AlignC
when do I need Align #PB_Structure_AlignC
macOS Catalina 10.15.7
Re: when do I need Align #PB_Structure_AlignC
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.
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
Re: when do I need Align #PB_Structure_AlignC
Thanks, thats the way I work, but it would be more easy if I understand the logic behind.
macOS Catalina 10.15.7
Re: when do I need Align #PB_Structure_AlignC
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.
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.
Re: when do I need Align #PB_Structure_AlignC
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
Re: when do I need Align #PB_Structure_AlignC
if I understand you correctly, I always have to use #PB_Structure_AlignC when I work with the API of the OS.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.
I've done alot without #PB_Structure_AlignC, but that was lucky it worked.
macOS Catalina 10.15.7
Re: when do I need Align #PB_Structure_AlignC
No, don't do that. You really have to check each structure individually, whether it needs to be aligned or not.Wolfram wrote:if I understand you correctly, I always have to use #PB_Structure_AlignC when I work with the API of the OS.
sorry for my bad english