Heap Fragmentation
Posted: 05 April 2008, 21:35 PM
What happens as variable length strings (or for that matter my own data) keep getting allocated and freed in the heap? Presumably when a block is freed, ajoining free space is collapsed into one free item on the free list. But if the heap is full but fragmented, there is not much that can be done except to free things and then reallocate them?
The motivation for this question is the best way to store up to 50 strings (or byte arrays) that can be added or deleted on the fly. There are 4 options:
1. Use vector of 50 strings which can lead to fragmentation of the heap
2. Use fixed length strings in an array which wastes space.
3. Use variable length character arrays that are allocated and deallocated in the heap.
4. Use a fixed length vector of bytes that I suballocate to each character string as needed and I collapse the gaps whenever a string is deleted. A separate unsigned integer vector of offsets into the one large vector of bytes is used to mark the beginning and each string. Another byte vector keeps the lengths.
The motivation for this question is the best way to store up to 50 strings (or byte arrays) that can be added or deleted on the fly. There are 4 options:
1. Use vector of 50 strings which can lead to fragmentation of the heap
2. Use fixed length strings in an array which wastes space.
3. Use variable length character arrays that are allocated and deallocated in the heap.
4. Use a fixed length vector of bytes that I suballocate to each character string as needed and I collapse the gaps whenever a string is deleted. A separate unsigned integer vector of offsets into the one large vector of bytes is used to mark the beginning and each string. Another byte vector keeps the lengths.