Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #118315 > unrolled thread

MSVC-bug

Started byBonita Montero <Bonita.Montero@gmail.com>
First post2024-02-13 12:07 +0100
Last post2024-03-02 12:38 -0800
Articles 10 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  MSVC-bug Bonita Montero <Bonita.Montero@gmail.com> - 2024-02-13 12:07 +0100
    Re: MSVC-bug Paavo Helde <eesnimi@osa.pri.ee> - 2024-02-15 15:34 +0200
      Re: MSVC-bug Bonita Montero <Bonita.Montero@gmail.com> - 2024-02-15 12:20 +0100
        Re: MSVC-bug Paavo Helde <eesnimi@osa.pri.ee> - 2024-02-16 00:11 +0200
          Re: MSVC-bug Bonita Montero <Bonita.Montero@gmail.com> - 2024-02-16 09:17 +0100
            Re: MSVC-bug Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-02-29 20:45 -0800
              Re: MSVC-bug Bonita Montero <Bonita.Montero@gmail.com> - 2024-03-01 14:53 +0100
                Re: MSVC-bug Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-03-01 18:44 -0800
                  Re: MSVC-bug Bonita Montero <Bonita.Montero@gmail.com> - 2024-03-02 07:36 +0100
                    Re: MSVC-bug Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-03-02 12:38 -0800

#118315 — MSVC-bug

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-02-13 12:07 +0100
SubjectMSVC-bug
Message-ID<uqfih1$22741$1@raubtier-asyl.eternal-september.org>
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();
}

[toc] | [next] | [standalone]


#118319

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2024-02-15 15:34 +0200
Message-ID<uql3tc$3atoh$1@dont-email.me>
In reply to#118315
13.02.2024 13:07 Bonita Montero kirjutas:
> 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.

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.

> 
> #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();
> }

[toc] | [prev] | [next] | [standalone]


#118320

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-02-15 12:20 +0100
Message-ID<uqlai8$3c3iu$1@raubtier-asyl.eternal-september.org>
In reply to#118319
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.

[toc] | [prev] | [next] | [standalone]


#118321

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2024-02-16 00:11 +0200
Message-ID<uqm260$3gd7u$1@dont-email.me>
In reply to#118320
15.02.2024 13:20 Bonita Montero kirjutas:
> 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.

[toc] | [prev] | [next] | [standalone]


#118322

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-02-16 09:17 +0100
Message-ID<uqn5m7$3p98f$2@raubtier-asyl.eternal-september.org>
In reply to#118321
Am 15.02.2024 um 23:11 schrieb Paavo Helde:

> 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.

[toc] | [prev] | [next] | [standalone]


#118335

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2024-02-29 20:45 -0800
Message-ID<urrmgq$12lh3$1@dont-email.me>
In reply to#118322
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.

-- 
Best regards,
Andrey

[toc] | [prev] | [next] | [standalone]


#118338

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-03-01 14:53 +0100
Message-ID<ursmlf$18r2d$1@raubtier-asyl.eternal-september.org>
In reply to#118335
Am 01.03.2024 um 05:45 schrieb Andrey Tarasevich:
> 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.

Not true ! This code prints "alloc" first with MSVC
and a Debug-build, but not with Release-builds.

#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]() { cout << str << endl; };
     fn = ref( lam );
     fn();
}

[toc] | [prev] | [next] | [standalone]


#118341

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2024-03-01 18:44 -0800
Message-ID<uru3ps$1i8fc$1@dont-email.me>
In reply to#118338
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`, and has nothing to do with `std::function<>`.

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 "---".

-- 
Best regards,
Andrey

[toc] | [prev] | [next] | [standalone]


#118344

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-03-02 07:36 +0100
Message-ID<uruhca$1ocbb$1@raubtier-asyl.eternal-september.org>
In reply to#118341
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.

> 
> 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 "---".
> 

[toc] | [prev] | [next] | [standalone]


#118345

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2024-03-02 12:38 -0800
Message-ID<us02nk$22aa3$1@dont-email.me>
In reply to#118344
On 03/01/24 10:36 PM, Bonita Montero wrote:
> 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.

... which is exactly what I did in the first place.

Once again: the Debug config of the _original_ version of the code emits 
two allocations, both of which originate in `std::string`. The first one 
is allocation of some sort of "container proxy" in `std::string`s 
constructor. Another is the same thing in your `[str = str]`.

The updated version of the code (with '[&str]`) emits only one 
allocation: the same "container proxy" in `std::string`s constuctor.

No allocations are made for `std::function<>` in either version.

-- 
Best regards,
Andrey

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web