If you initialize a function<>-object with a reference_wrapper it
isÿ guaranteed that the function<>-object references an external function-object without any memory allocation. libstdc++ and libc++
don't allocate any memory with such function<>-objects according
to the standard, but MSVC does allocate external memory.
#include <iostream>
#include <functional>
using namespace std;
void *operator new( size_t n )
{
ÿÿÿÿcout << "alloc" << endl;
ÿÿÿÿvoid *p = malloc( n );
ÿÿÿÿif( !p )
ÿÿÿÿÿÿÿ throw bad_alloc();
ÿÿÿÿreturn p;
}
void operator delete( void *p )
{
ÿÿÿÿfree( p );
}
int main()
{
ÿÿÿÿfunction<void ()> fn;
ÿÿÿÿstring str( "hello word" );
ÿÿÿÿauto lam = [str = str]() { cout << str << endl; };
ÿÿÿÿfn = ref( lam );
ÿÿÿÿfn();
}
Cannot confirm that. MSVC++ 2022 x64 Release mode does not print "alloc"
at all for this program. In Debug mode there are a couple of allocs, but these seem to be related to some std::string internals, not the
reference wrapper.
Am 15.02.2024 um 14:34 schrieb Paavo Helde:
Cannot confirm that. MSVC++ 2022 x64 Release mode does not print
"alloc" at all for this program. In Debug mode there are a couple of
allocs, but these seem to be related to some std::string internals,
not the reference wrapper.
Sorry, I relied on the Debug build.
With Release build there are no allocations.
But I think the Debug build should also be up to the standard.
Constructing and copying std::strings is allowed to allocate dynamic
memory. Make your string a bit longer and you will see allocations
also in Release builds.
The small string optimization is nothing which is guaranteed.
Initializing a function object with a reference_wrapper is
guaranteed to be without memory allocation.
On 02/16/24 12:17 AM, Bonita Montero wrote:
The small string optimization is nothing which is guaranteed.
Initializing a function object with a reference_wrapper is
guaranteed to be without memory allocation.
Why do you keep spewing this nonsense?
You have been told very clearly already that in this case
`std::function<>` does not perform any memory allocation in Visual
Studio. Neither in Debug nor in Release builds. You imagined it.
You have been told very clearly already that in this case
`std::function<>` does not perform any memory allocation in Visual
Studio. Neither in Debug nor in Release builds. You imagined it.
Not true ! This code prints "alloc" first with MSVC
and a Debug-build, but not with Release-builds.
On 03/01/24 5:53 AM, Bonita Montero wrote:
???
You have been told very clearly already that in this case
`std::function<>` does not perform any memory allocation in Visual
Studio. Neither in Debug nor in Release builds. You imagined it.
Not true ! This code prints "alloc" first with MSVC
and a Debug-build, but not with Release-builds.
As you have already been told, your "alloc" originates from
`std::string`, ...
Do this
ÿ ...
ÿ int main()
ÿ {
ÿÿÿ function<void ()> fn;
ÿÿÿ string str( "hello word" );
ÿÿÿ auto lam = [&str]() { cout << str << endl; };
ÿÿÿ cout << "---" << endl;
ÿÿÿ fn = ref( lam );
ÿÿÿ cout << "---" << endl;
ÿÿÿ fn();
ÿ }
Your "alloc" is printed before the first "---".
Am 02.03.2024 um 03:44 schrieb Andrey Tarasevich:
On 03/01/24 5:53 AM, Bonita Montero wrote:
???
You have been told very clearly already that in this case
`std::function<>` does not perform any memory allocation in Visual
Studio. Neither in Debug nor in Release builds. You imagined it.
Not true ! This code prints "alloc" first with MSVC
and a Debug-build, but not with Release-builds.
As you have already been told, your "alloc" originates from
`std::string`, ...
No, from the function allocation. Just single-step the code yourself.
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 6 |
Nodes: | 8 (0 / 8) |
Uptime: | 21:35:31 |
Calls: | 45 |
Files: | 21,492 |
Messages: | 63,008 |