http://qs1969.pair.com?node_id=11148340


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

Hi Rob!

I think both the OP and you are not quite right about allocating memory for EdjeMessageStringSet. In my post below, see:

EdjeMessageStringSet* m = (EdjeMessageStringSet*) safemalloc( sizeof(E +dje_Message_String_Set) + (count-1)*sizeof(char *) ); if (m == NULL) croak("Failed to allocate memory in _new function\n");
First, we have to talk about what this means:
typedef struct { int count; char *str[1]; // may be fine with [] or even just char* str (no su +bscript at all) } Edje_Message_String_Set;
Normally a type has a fixed size that is known at compile time and sizeof() works just fine. That is not true in this case. Normally, I would have expected to use a fixed size Edje_Message_String_Set, like this:
typedef struct { int count; char** string_array; } Edje_Message_String_Set;
Now Edje_Message_String_Set is a fixed size. To instance one, you allocate memory for the type with sizeof(). Then you allocate memory for an array of pointers to strings with size of count. You put the address of this dynamically allocated array into the variable string_array.

In theory, you can save one call to malloc() for dynamic array allocation and potentially the space for the char**, by allocating the dynamic array directly inside the Edje_Message_String_Set type rather than having a pointer to the dynamically allocated array.

This means that you can't use a memory allocation I/F that says give me say 5 of type X. Give a "number of a type" and a type is not sufficient here because the sizeof() the type is actually unknown at compile time.

To put some numbers on this. Rob, you have a 64 bit machine, so for count of 1, we get 16 bytes. 8 bytes for int and 8 bytes for pointer to char. If say count==3. Then we need to allocate an additional 16 bytes for 2 more char*'s. Hence, we arrive at my safemalloc() math shown above. The first 16 bytes comes from sizeof(Edje_Message_String_Set) but that is only the minimum size to cover the size of 1. Now if we had allocated say 2 * sizeof(Edje_Message_String_Set), that gives us enough space for count==3, not 2!