Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, just as in C, you can't do exactly what you do want. The only useful way of working with a structure that does what you want in C is to have it as <code>struct Points { int num_points; Point *points; }</code></p> <p>And have utility code to alloc you memory where you can put your data. Unless you have some safe maxsize, and don't want to bother with that part of the code (memory allocation) - the network part of the code would then transmit just the needed data from within the structure, not the whole of it. </p> <p>To work with Python ctypes with a structure member which actually contains a pointer to where your data is (and so, may be of variable length) - you will also have to alloc and free memory manually (if you are filling it on the python side) - or just read the data - f creating and destroying the data is done on native code functions.</p> <p>The structure creating code can be thus:</p> <pre><code>import ctypes as c class Point(c.Structure): _fields_ = [ ('x',c.c_double), ('y',c.c_double), ('z',c.c_double) ] class Points(c.Structure): _fields_ = [ ('num_points', c.c_uint32), ('points', c.POINTER(Point)) ] </code></pre> <p>And the code to manage the creation and deletion of these data structures can be:</p> <pre><code>__all_buffers = {} def make_points(num_points): data = Points() data.num_points = num_points buf = c.create_string_buffer(c.sizeof(Point) * num_points) __all_buffers[c.addressof(buf)] = buf p = Point.from_address(c.addressof(buf)) data.points = c.pointer(p) return data def del_points(points): del __all_buffers[c.addressof(m.points[0]) points.num_points = 0 </code></pre> <p>The use f the global variable "__all_buffers" keep a reference to the python-created buffer object so that python does not destroy it upon leaving the make_points structure. An alternative to this is to get a reference to either libc (on unixes) or winapi,and call system's <code>malloc</code> and <code>free</code>functions yourself</p> <p>OR - you can just go with plain old "struct" Python module, instead of using ctypes - doubly so if you will have no C code at all, and are just using ctypes for the "structs" convenience.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload