Re: SWIG: Bug with 'C' arrays of zero size ?

William S Fulton <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftCoSjuNOfrP2Qcqu+e9dQZngZ=SKVz3=kSMhqn5wL03CQ@mail.gmail.com>
On Sat, 27 Jun 2020 at 10:31, Terry Barnaby <[email protected]> wrote:

> On 27/06/2020 09:58, William S Fulton wrote:
>
>
>
> On Fri, 12 Jun 2020 at 09:37, Terry Barnaby <[email protected]> wrote:
>
>> Hi,
>>
>> If you have a data structure like the following all is well:
>>
>> struct DataBlock {
>>      Header    header;
>>      char        data[4];    ///< The packet data
>> };
>>
>> However if you have a data structure that has a zero length array like
>> the following, SWIG considers the "char data[]" as a pointed to data and
>> tries to delete[] and allocate memory for it.
>>
>> struct DataBlock {
>>     Header    header;
>>      char        data[];    ///< The packet data
>> };
>>
>> I think this is a bug ?
>>
>>
> Hi Terry
>
> This is a pointer to an undetermined array of char characters. By default
> SWIG chooses to wrap this a pointer to a NULL terminated array of
> characters, that is, a string. There is no bug here!
>
> You can use typemaps to change the behaviour to whatever you want.
>
> William
>
> But actually the data member is not a pointer to an array of characters,
> the actual array is contained within the structures memory area as part of
> the data structure (although 0 length!).
>
That is incorrect, it is a pointer to an undefined array size.


> So I would have though that SWIG should handle this just as if you had a
> size to this. Admittedly this is probably mute as you may not be able to
> wrap that member correctly in SWIG anyway :)
>
Fundamental to understanding SWIG is many types in C and C++ are ambiguous
as to what they are or contain. char[] is a classic case. SWIG's approach
is to assume (the most common) usage and intent and wrap it accordingly and
to provide customisation techniques to treat it in some other way. So in
this case you need to provide the customisations for the generated code.

William

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.