Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: auto_ptr fo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 11 of 16 Topic 9517 of 9831
Post > Topic >>

Re: auto_ptr for array of built-ins

by Lance Diduck <lancediduck@[EMAIL PROTECTED] > Apr 18, 2008 at 05:17 PM

On Apr 17, 2:43 pm, Carlos Moreno <cm_n...@[EMAIL PROTECTED]
> wrote:
> Hi,
>
> I'm facing a situation where it would seem all possible solutions
> have some severe inconvenience.
>
If N is known at compile time:
template<std::size_t N>struct Buf{
   //maybe a length member?
   char mbuf[N];
};
std::auto_ptr<Buf<250> > mybuf(new Buf<250>);
yeilds exactly what you want. (of course, passing mybuf->mbuf to your
function)

If N is not known at compile time:
void *p=::operator new(n+sizeof(Buf<1>));
std::auto_ptr<Buf<1> > mybuf(new (p)Buf<1> );
also gives you what you want.

As noted by other poesters there are fancier pointers out there, and
other ways of solving the problem, but the above is how you might do
it with the humble auto_ptr.

Lance





-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 16 Posts in Topic:
auto_ptr for array of built-ins
Carlos Moreno <cm_news  2008-04-17 12:43:22 
Re: auto_ptr for array of built-ins
red floyd <no.spam@[EM  2008-04-17 16:19:42 
Re: auto_ptr for array of built-ins
Howard Hinnant <howard  2008-04-17 16:22:24 
Re: auto_ptr for array of built-ins
markus2004x@[EMAIL PROTEC  2008-04-17 19:31:52 
Re: auto_ptr for array of built-ins
Carl Barron <cbarron41  2008-04-17 19:35:28 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-17 19:33:40 
Re: auto_ptr for array of built-ins
Tony Delroy <tony_in_d  2008-04-18 06:13:14 
Re: auto_ptr for array of built-ins
Howard Hinnant <howard  2008-04-18 14:59:22 
Re: auto_ptr for array of built-ins
Carlos Moreno <cm_news  2008-04-18 15:02:34 
Re: auto_ptr for array of built-ins
Pete Becker <pete@[EMA  2008-04-18 17:16:33 
Re: auto_ptr for array of built-ins
Lance Diduck <lancedid  2008-04-18 17:17:38 
Re: auto_ptr for array of built-ins
mark.zaytsev@[EMAIL PROTE  2008-04-18 17:18:22 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-19 02:02:07 
Re: auto_ptr for array of built-ins
"Bo Persson" &l  2008-04-19 18:13:15 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-20 15:48:01 
Re: auto_ptr for array of built-ins
"Martin T." <  2008-04-22 11:13:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:52:15 CDT 2008.