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 » Doubt with Buffer<> of a trivially destructible type
Re: Doubt with Buffer<> of a trivially destructible type [message #61224 is a reply to message #61223] Thu, 12 December 2024 16:05 Go to previous messageGo to previous message
koldo is currently offline  koldo
Messages: 3437
Registered: August 2008
Senior Veteran
Thank you Oblivion

Yes, there are simple solutions. This case is to alloc data to be filled by a DLL made in Delphi, with particular alignment requirements.

I have reviewed deeper in how U++ handles this, and it takes care of that in run time:
void Free() {
	if(ptr) {
		if(std::is_trivially_destructible<T>::value)  // <<========
			MemoryFree(ptr);
		else {
			void *p = (byte *)ptr - 16;
			size_t size = *(size_t *)p;
			Destroy(ptr, ptr + size);             // <<======== Destroy() is called only if there is a destructor
			MemoryFree(p);
		}
	}
}

However the compiler detects this:
template <class T>
inline void Destruct(T *t)
{
	t->~T();                        // error: object expression of non-scalar type 'double[3]' cannot be used in a pseudo-destructor expression
}

template <class T>
inline void Destroy(T *t, const T *end)
{
	while(t != end)
		Destruct(t++);
}


Best regards
IƱaki
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to respond when memory is exceeded
Next Topic: Stream Load serialization fired twice
Goto Forum:
  


Current Time: Sun Jun 08 01:33:10 CEST 2025

Total time taken to generate the page: 0.04451 seconds