Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^8: Perl XS binding to a struct with an array of chars*

by syphilis (Archbishop)
on Nov 27, 2022 at 05:23 UTC ( [id://11148401]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Perl XS binding to a struct with an array of chars*
in thread Perl XS binding to a struct with an array of chars*

Nice work !!
I'll draw attention to an oddity I've just noticed, of perhaps little significance.
Regarding the struct definition and typedef:
struct _Edje_Message_String_Set { int count; //On 64 bit machine, this is 8 bytes char *str[]; //str has no "size" and is not counted in sizeof(_Edj +e_Message_String_Set) }; typedef struct _Edje_Message_String_Set EdjeMessageStringSet;
The 'int' type is always 4 bytes on Windows, irrespective of architecture. And I think it's generally the same case on Linux.
IIRC, on Linux, it's usually the size of the 'long int' that varies with architecture - but 'int' usually stays at 4 bytes.

On 64 bit Windows, I'm finding that if char * str[]; is removed from the struct, then struct size is 4 bytes.
If char * str[]; is included, then the struct size is 8 bytes.
So it seems that "str" is increasing the size of the struct by 4 bytes. But that doesn't seem right to me.

Here's the demo I used:
use strict; use warnings; use Inline C => Config => BUILD_NOISY => 1, USING => 'ParseRegExp', ; use Inline C =><<'EOC'; typedef struct _foo { int count; } foo; typedef struct _bar { int count; char *str[]; } bar; void sizes(void) { printf( "FOO: %d %d\n", sizeof(struct _foo), sizeof( foo) ); printf( "BAR: %d %d\n", sizeof(struct _bar), sizeof( bar) ); printf( "INTSIZE: %d\n", sizeof(int) ); } EOC sizes(); __END__ On 64 bit windows, outputs: FOO: 4 4 BAR: 8 8 INTSIZE: 4
Maybe a bug in xsubpp ? Nope - it's just something that 64-bit gcc does on both Windows and Ubuntu. If it's a bug, then it's a gcc bug.

On 32-bit windows, it seems that char *str[]; does indeed make zero contribution to the size of the struct, and the same script outputs:
FOO: 4 4 BAR: 4 4 INTSIZE: 4
Cheers,
Rob

Replies are listed 'Best First'.
Re^9: Perl XS binding to a struct with an array of chars*
by Marshall (Canon) on Nov 28, 2022 at 07:31 UTC
    I was surprised to learn that size of int is 32 bits even on a 64 bit compiler. But that is true. To get 64 bit int, even long long is required on some compilers.

    What I found out is that default for the 64 bit gcc compiler is to enable padding to maintain dword memory alignment in structures. This is what is causing the seemingly weird results with your sizeof() test code. sizeof() can return more than the obvious number of bytes due to padding. See Data Alignment in Structs for some more info. There is a way in gcc to override this behavior if perhaps you need to match some weird binary structure exactly verbatim. Just like malloc(), struct address assignment "likes" dword (64 bit alignment).

    typedef struct _foo { int count; //with just int, sizeof() is 4 bytes char anything; //forces sizeof() increase from 4 to 8 bytes! } foo;
    Evidently, even putting something in the struct that has no storage, like char* pointer[], forces alignment bytes to be added. The mysterious extra 4 bytes aren't anything, they are just junk padding bytes. The pointer(s) when added will be 8 bytes and it is highly desirable for these to be fully contained on a single memory row. If they are on 32 bit boundaries, the hardware can still read them, but at a performance penalty.

    So my code as written performs correctly, however the explanation of why the struct is 8 bytes is not correct. 4 bytes are for the integer (not 8 as I wrongly assumed) but then an additional 4 bytes are added as padding. 4+4=8. So the entire array of pointer is 64 bit aligned (8 byte boundary).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148401]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found