Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Vector::SetCount()
Vector::SetCount() [message #19096] Wed, 12 November 2008 13:38 Go to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Until very recently I hadn't realised that SetCount actually deallocates memory when the count is less than the allocation. This means you can't use a Vector for an iteration of an algorithm, then reuse the memory for the next iteration without deallocating/reallocating it.

This isn't a major issue in itself as I can always hack it in myself if necessary, but there is currently no mention of the deallocation in the documentation. This is particularly misleading because the allocation of extra space is specifically mentioned:
Quote:

Changes count of elements in Vector to specified value. If required number of elements is greater than actual number, newly added elements are default constructed. If Vector has to increase capacity, the new capacity will exactly match required number of elements (unlike SetCountR).

Perhaps it could be changed? And would it be possible to add a function that allows setting of items without deallocation?
Re: Vector::SetCount() [message #19109 is a reply to message #19096] Wed, 12 November 2008 22:09 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Are you sure? Do not see anything like that in the code:

template <class T>
void Vector<T>::Trim(int n)
{
	ASSERT(n >= 0 && n <= items);
	DestroyArray(vector + n, vector + items);
	items = n;
}

template <class T>
void Vector<T>::SetCount(int n) {
	Chk();
	ASSERT(n >= 0);
	if(n == items) return;
	if(n < items)
		Trim(n);
	else {
		if(n > alloc) ReAllocF(n);
		ConstructArray(vector + items, vector + n);
		items = n;
	}
}

Re: Vector::SetCount() [message #19122 is a reply to message #19109] Thu, 13 November 2008 13:42 Go to previous message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Embarassed Apologies, I had assumed DestroyArray was doing deallocation without checking. Foolish mistake.
Previous Topic: Win 98
Next Topic: [Solved] Any::dtor fails after it is picked
Goto Forum:
  


Current Time: Sun Apr 28 02:38:17 CEST 2024

Total time taken to generate the page: 0.05254 seconds