سؤال

20.6.11 Temporary buffers [temporary.buffer] defines two function templates:

template<class T> pair<T*, ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;

template<class T> void return_temporary_buffer(T* p);

Is there something similar in the C standard? Something like:

void * get_temporary_buffer(size_t);

void return_temporary_buffer(void *);

And no, malloc/free does not count as an answer ;)

هل كانت مفيدة؟

المحلول

There is not something similar in the C standard. The standard says this about get_temporary_buffer:

Obtains a pointer to storage sufficient to store up to n adjacent T objects.

I.e. you are not guaranteed to get the space you request. Most C++ standard library implementations today implement get_temporary_buffer() as a simple new-based memory allocation, that if it fails, is repeated with smaller and smaller allocations sizes. Implementing something with the same effect in C would not be hard.

نصائح أخرى

As mentioned on http://en.wikipedia.org/wiki/C_dynamic_memory_allocation, the section Implementations do listed lots of implementation based on C for dynamic memory allocation, which can be see as some alternative as C++ get_temporary_buffer maybe?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top