Jump to content
  • Advertisement
Sign in to follow this  
ExErvus

Is This Evil(Reserve Memory In Template Linked List)

This topic is 801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This would only work if T is a pointer to type, which I believe I would have to use Template specialization to solve compile time issues with stack variables(although I'm not to keen on how to go about that).

 

Anyways, this is how I reserve memory in my list

template <class T>
void LWSList<T>::Reserve(int elements)
{
	if(elements <= 0)
		return;

	if(!_head && !_tail)
	{
		_head = (LWSList<T>::ListItem*)malloc(sizeof(LWSList<T>::ListItem) * elements);
		_tail = _head + (elements - 1);

		//Link elements together and create data

		//Head
		LWSList<T>::ListItem* itr = _head;
		_head->linkForward = _head + 1;
		_head->data = (T)malloc(sizeof(T));
			
		//Rest of list
		for(int i = 1; i < elements; i++)
		{
			itr++;

			itr->data = (T)malloc(sizeof(T));
			if(itr == _tail)
			{
				itr->linkBack = itr - 1;
			}
			else
			{
				itr->linkForward = itr + 1;
				itr->linkBack = itr - 1;
			}			
		}

		//set new size of list
		_size = elements;
	}
}

And this is how I fill it

//TODO this causes intellisense to freakout when debuggin, ??? for data value in intellisense when paused
template <class T>
void LWSList<T>::Fill(void* source)
{
	//Parse source
	char* buffer = (char*)source;

	LWSList<T>::ListItem* itr = _head;
	for(int i = 0; i < _size; i++)
	{
		memcpy((void*)itr->data, buffer + (i * sizeof(T)), sizeof(T));

		itr++;
	}
}

If you notice the comment on my Fill() function, this causes intellisense to question mark the value during run time. I cant view it while I debug, but it works. So is the way I am going about this completely crazy or am I on track? The objective is to be able to Reserve n number of elements of type T and fill it in.

 

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Apart from the fact that you seem to be throwing away type information needlessly what worries me first and foremost is trying to memcpy stuff around. The only types you are allowed to do that with are types for which std::is_trivially_copyable evaluates to true.

Additionally, I'm not really sure what you are trying. I have the strong suspicion though you might be better served with either std::deque or by using a standard std::list with a custom pool-based allocator.

Share this post


Link to post
Share on other sites

The objective is to be able to Reserve n number of elements of type T and fill it in.


Consider using the standard library containers, their reserve() functions, and generating data with functions like generate() or generate_n(), fill() or fill_n().

As it is your Reserve() only somewhat works if you've got an empty list already, uses the very slow global allocator, and has some nasty bugs which BitMaster indicated a few of. It looks like you also leave your linkForward dangling on your last item, and don't handle any errors that can occur, the code blindly assumes all standard library calls succeed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!