home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,785 of 33,346   
   Juan Pedro Bolivar Puente to Dave Abrahams   
   Re: Lambda allocations   
   06 Jan 12 17:25:32   
   
   From: raskolnikov@gnu.org   
      
   On 03/01/12 07:12, Dave Abrahams wrote:   
   > on Mon Jan 02 2012, rgba32  wrote:   
   >   
   >> Lambdas are a new-ish topic for me. As I dive in an learn more about   
   >> them, it occurred to me that there may be some "hidden" memory   
   >> allocations happening to account for the cases where a closure and   
   >> it's state need to outlive the scope it was created in.   
   >   
   > Nope, there is no reason at all for an implementation to generate   
   > "hidden allocations."  The closure is exactly equivalent to an instance   
   > of a class that you can write down by inspecting the code of the   
   > function in which it appears.  If you could look at the member functions   
   > of that class you would not see any allocation or deallocation.   
   >   
   >> Looking at the assembly output of a little lambda test app compiled   
   >> with VC10, I do indeed see a call to operator new in this very   
   >> case.   
   >   
   > I can't imagine that it has anything directly to do with your closure.   
   > Probably you are capturing by value a local variable that does dynamic   
   > allocation in its copy constructor.   
   >   
      
   The OP is right.  There is no way for a lambda to outlive the scope it   
   was created in without dynamic memory allocation.  The trick in C++11 is   
   that to return a lambda you hace to use std::function<>, where the   
   allocation is actually performed.   
      
   JP   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca