#30 Variable bitfields not initializing after some C data types

open
None
5
2008-05-21
2008-05-19
No

This structure will not work correctly:

class TestA(Structure):
_fields_ = (
('ch', c_char),
('a', c_uint, 2),
('b', c_uint, 2),
('c', c_uint, 2),
('d', c_uint, 2),
)

But this one works fine:

class TestB(Structure):
_fields_ = (
('ch', c_char),
('a', c_uint8),
)

If you make an instance of TestB and then make TestA from TestB's address TestA's a,b,c,d values will all be set to zero. But if c_char is changed to something like c_char_p everything will work fine.

Ubuntu Linux
x86
ctypes 1.0.2
python 2.5

I'm trying to port a library, and would really appreciate it if this was fixed. Many thanks in advance.

I added a test script I used to make sure this error was consistent.

Discussion

  • Kyle Stewart

    Kyle Stewart - 2008-05-19

    Demonstration script

     
  • Kyle Stewart

    Kyle Stewart - 2008-05-19

    Logged In: YES
    user_id=2091170
    Originator: YES

    I also found that c_byte is causing the same problem.

     
  • Kyle Stewart

    Kyle Stewart - 2008-05-20

    Logged In: YES
    user_id=2091170
    Originator: YES

    c_char, c_byte, c_short, c_ubyte, and c_ushort are the causes. Everything else is working fine.

    The problem is only when the object is initialized. When that happens the bit fields after the data type are cleared to zeros but can then be used normally without problems. Bit fields before the data type are not effected. If a working data type is between a not working data type and the variable bit fields then the bit fields will still work.

    Then things become very inconsistent when working data types are put in between different bit fields.

    I was able it get around the problem by replacing a ('ch', c_char) before a bit field with a ('ch', c_uint, 8) but I don't think that will work for every structure.

    I hope this information helps.

     
  • Kyle Stewart

    Kyle Stewart - 2008-05-20
    • summary: Unexpected behavior in variable size bitfields after c_char --> Variable bitfields not initializing after some C data types
     
  • Thomas Heller

    Thomas Heller - 2008-05-21
    • assigned_to: nobody --> theller
     
  • Thomas Heller

    Thomas Heller - 2008-05-21

    Logged In: YES
    user_id=11105
    Originator: NO

    Ok, I can reproduce the behaviour on windows. Thanks for the clear report, I will look into it.

    First comment: I am not sure this is a bug or not, I will investigate what the C compiler does.
    One difference between your structure A and B is that the size is different, also the alignment of the second field is different.

    The problem also goes away when you replace the 'c_int' in structure A with 'c_uint8' (that may or may not be a workaround for you).

     

Log in to post a comment.