Shrink allocated memory with 'new'.

I have 2 sets created with 'new': (symbolic write)
set1 = {10, 15, 20, 21, 50}
set2 = {1, 5, 20, 21, 150}
I want to create the sum of sets so I allocate with new int[sizeof(set1) + sizeof(set2)] which is 5+5=10 positions.
but the addition is:
set3 = {1, 5, 10, 15, 20, 21, 50, 150}
and it has 8 positions.
Can I shrink, after the addition operation, the heap block, without copying everything to a new buffer? Like realloc.

I use something like this in really big SparseVector and SparseMatrix so I don't want to use std::vector because it has 3 members instead of 2 than I use. (std::vector uses an extra member for capacity())
closed account (zb0S216C)
chameleon wrote:
"Can I shrink, after the addition operation, the heap block, without copying everything to a new buffer? Like realloc."

No; you'll have to do it manually.

chameleon wrote:
"I don't want to use std::vector because it has 3 members instead of 2 than I use."

Two members are for marking the end of the allocated region, while the other is for marking the next available slot. All three are required to create a safe storage container.

Wazzak
Last edited on
Thanks.

About std::vector members, they are:
start_pos
end_pos
capacity_pos

of course implementations can be differ.
closed account (zb0S216C)
Indeed. Though, they behave the same - irrespective of the implementation. I implement my dynamic containers in this way.

Wazzak
Last edited on
Topic archived. No new replies allowed.