ucx icon indicating copy to clipboard operation
ucx copied to clipboard

UCS/DATASTRUCT: disable memcopy when passing _old_buffer_p to array_grow

Open roiedanino opened this issue 1 year ago • 2 comments

What

Skip calling memcpy when passing _old_buffer_p to ucs_array_append_safe / ucs_array_reserve_safe, making it callers responsibility copying the data to the new allocated buffer.

Why ?

Some arrays may contain pointers or pointer-based data structures (e.g. linked lists), for which amemcpy won't do, and the inner data structures would break.

roiedanino avatar Jun 04 '24 12:06 roiedanino

General comment. I understand the importance of this fix, but it still looks a bit strange to me, because of 2 things:

  1. IMO it is still error prone to leave the copy part to the caller. It means that other existing API functions would never work properly: ucs_array_append, ucs_array_resize. If you go this way, then maybe during array initialization we may keep a function pointer to the array-resize. Then it will be called automatically each time array is re-allocated with the new address Still this is not what I would propose..
  2. If someone has a collection of objects of complex type, then the obvious choices are:
  • just store array of pointers, not objects, so that objects content is never copied
  • store them in the linked list Because it looks like the wrong data structure choice, if you need to copy the whole content of the vector

iyastreb avatar Jun 17 '24 06:06 iyastreb

Per discussion with @roiedanino and @iyastreb , we'll only support "trivial" types without custom user copy ctor + need to add a comment in docs.

gleon99 avatar Jun 17 '24 08:06 gleon99