title: python ctypes array of structs
body: <p>I have the followng code and it ends up to segmentation fault. </p>
  
  <pre><code>import ctypes
  from random import randint
  
  class STRUCT_2(ctypes.Structure):
      #_pack_=2
      _fields_ = [('field_1', ctypes.c_short),
                  ('field_2', ctypes.c_short),
                  ('field_3', ctypes.c_short)]
  
  class STRUCT_1(ctypes.Structure):
      #_pack_=2
      _fields_ = [('elements', ctypes.c_short),
                  ('STRUCT_ARRAY', ctypes.POINTER(STRUCT_2))]
  
      def __init__(self,num_of_structs):
          elems = (ctypes.POINTER(STRUCT_2) * num_of_structs)()
          self.STRUCT_ARRAY = ctypes.cast(elems,ctypes.POINTER(STRUCT_2))
          self.elements = num_of_structs
  
          for num in range(0,num_of_structs):
              self.STRUCT_ARRAY[num].field_1 = 1
              self.STRUCT_ARRAY[num].field_2 = 2
              self.STRUCT_ARRAY[num].field_3 = 3
  
  for num in range(0,100):
      test = STRUCT_1(num)
      print "%i done" % num
  </code></pre>
  
  <p>output: 
  5 done
  Segmentation fault</p>
  
  <p>But if field_3 is not present in struct_2 then it seems to work correctly. If I add one short field (field_4) more it ends to segmentation fault... </p>
  
  <p>So what i'am doing incorrectly or what i have missed here??</p>
  
  <p>Is there someother way to define array size?</p>