2 ms·
Thank you very much for taking a look into the code :-D Regarding identity, you can use double pointers. In the case of willing to eliminate leakage risks (or
by faragon 11y ago
Thank you very much for taking a look into the code :-D
Regarding identity, you can use double pointers.
In the case of willing to eliminate leakage risks (or speed-up the operation, avoiding calling the heap allocator), you can use stack allocation (using sv_alloca instead of sv_alloc), with implicit free, e.g.
{
sv_t *a = sv_alloca_t(SV_INT32, 42);
sv_t *b = sv_alloca_t(SV_INT32, 42);
sv_t *c = sv_alloca(sizeof(struct mystruct), 42);
struct mystruct { int r, s; }
y = { 1, 2 }, z = { 3, 4 };
sv_push_i(&a, 0);
sv_push_i(b, 1); sv_push_i(b, 2); sv_push_i(b, 3);
sv_cat(&a, b);
sv_cat(&a, a, b);
sv_push(&c, &y, &z, &y, &z, &y, &z);
sv_cat(&c, c);
/* implicit free */
}
Also, please note that is a C library, so the only "RAII"-like implicit freeing is only possible using stack allocation (or compiler-specific stack cleanup callbacks). In my opinion is not worth it to encapsulate it for C++, as for C++ the STL library is already very good for most cases.
With heap allocation (you could mix both):
{
/* Using the heap, you can start with size 0,
* the vector will grow automatically:
*/
sv_t *a = sv_alloc_t(SV_INT32, 0);
sv_t *b = sv_alloc_t(SV_INT32, 0);
sv_t *c = sv_alloc(sizeof(struct mystruct), 0);
struct mystruct { int r, s; }
y = { 1, 2 }, z = { 3, 4 };
sv_push_i(&a, 0);
sv_push_i(b, 1); sv_push_i(b, 2); sv_push_i(b, 3);
sv_cat(&a, b);
sv_cat(&a, a, b);
sv_push(&c, &y, &z, &y, &z, &y, &z);
sv_cat(&c, c);
/* explicit free */
sv_free(&a, &b, &c);
}