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 32,116 of 33,346   
   Dave Abrahams to cppljevans-AT-gmail.com   
   Re: allocate memory 'inside' POD   
   11 Apr 12 13:35:40   
   
   c753ba84   
   From: dave@boostpro.com   
      
   on Wed Apr 11 2012, cppljevans-AT-gmail.com wrote:   
      
   > On Saturday, April 7, 2012 12:46:32 AM UTC-5, Dave Abrahams wrote:   
   > [snip]   
   >> But if that makes you uneasy, consider:   
   >>   
   >>      union foo   
   >>      {   
   >>          int nVals;   
   >>          boost::aligned_storage<   
   >>              sizeof(int),   
   >>              boost::alignment_of::value   
   >>          >::type align;   
   >>      };   
   >>   
   >> now you could treat the memory starting at &x+1 (where x is of type   
   >> foo) as an array of doubles:   
   > [snip]   
   > Hi Dave,   
   >   
   > IIUC, the purpose of using aligned_storage with the   
   > alignment_of arg is to make sure the union starts at an   
   > alignment suitable for a double.  However, since memory   
   > for the nVals and the array of doubles is to be   
   > allocated with new(as shown by the OP):   
   >   
   >   foo * fooPtr = static_cast(::operator new(memToAlloc));   
   >   
   > and since new always (IIUC) returns memory allocated   
   > at the maximum alignment, I'm wondering why the aligned_storage   
   > and alignment_of are needed.   
      
   It ensures that (fooPtr+1) is aligned appropriately for storing doubles.   
      
   --   
   Dave Abrahams   
   BoostPro Computing   
   http://www.boostpro.com   
      
      
         [ 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