www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does this allocate?

reply "ponce" <contact gam3sfrommars.fr> writes:
I'm wondering if a delegate which theoretically doesn't need to 
make a heap closure, might allocate anyway.
Somehow I don't want to assume the compiler will do the 
non-allocating way.

This is the code I wrote:
---

     bool feed(float x, Complex!float[] fftData)
     {
         void processSegment(float[] segment)
         {
             fftData.length = _fftSize; // we do need the delegate 
context here

             /* do stuff with segment and fftData */
         }

         // _analyzer.feed takes a void delegate(float[]) as 
parameter
         // will this line allocate?
         return _analyzer.feed(x, &processSegment);
     }

---

Does the last line allocate?
Mar 23 2014
parent Nick Treleaven <ntrel-public yahoo.co.uk> writes:
On 23/03/2014 23:01, ponce wrote:
 I'm wondering if a delegate which theoretically doesn't need to make a
 heap closure, might allocate anyway.
 Somehow I don't want to assume the compiler will do the non-allocating way.

 This is the code I wrote:
 ---

      bool feed(float x, Complex!float[] fftData)
      {
          void processSegment(float[] segment)
          {
              fftData.length = _fftSize; // we do need the delegate
 context here

              /* do stuff with segment and fftData */
          }

          // _analyzer.feed takes a void delegate(float[]) as parameter
          // will this line allocate?
          return _analyzer.feed(x, &processSegment);
      }

 ---

 Does the last line allocate?
In theory, if you use 'scope void delegate(float[])', it won't allocate. But with scope or not, it's probably best to inspect the assembly output if you need to know for sure. Perhaps using http://d.godbolt.org/ can help you do that.
Mar 27 2014