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 7 of 16 Topic 9517 of 9775
Post > Topic >>

Re: auto_ptr for array of built-ins

by Tony Delroy <tony_in_da_uk@[EMAIL PROTECTED] > Apr 18, 2008 at 06:13 AM

On Apr 18, 3:43 am, Carlos Moreno <cm_n...@[EMAIL PROTECTED]
> wrote:
> I'm creating a C++ wrapper for a C library;  in this one function,
> I need to return a string.  The underlying C library expects me
> to pass it a pointer where to put the result;  that pointer needs
> to be dynamically allocated in the general case.
>
> So, my function goes more or less like this:
>
> string f()
> {
>      char * result = new char [ ... ];
>
>      int length = call_c_function (result,  .... other
> parameters ... );
>          // the function places the result string in the memory
>          // pointed to by result, and it returns the length of the
> string
>
>      return string (result, result + length);
>
> }

I won't enter into the question of whether your performance concerns
are justifiable - I'm only prepared to present what I consider good
practice when they are.

There are three scenarios:
1) you know a maximum buffer size you need to sup****t at compile time:
    string f()
    {
       char buffer[MAX];
       int len = c_function(buffer, ...);
       return string(buffer, buffer + len);
    }

2) you know the maximum buffer size at run-time and before calling the
C function:

Many compilers will actually accept this:

    string f(size_t max)
    {
      char buffer[max];
      <as above>

Alternatively, try this:

    #include <alloca.h>

    string f(size_t max)
    {
      char* buffer = alloca(max);
      <as above>

3. You don't know the buffer size requirements before calling the C
function:

Some C functions (e.g. snprintf() variants), return the untruncated
length even when truncating the result to the caller-provided buffer.
This can then be used by the caller to allocate a bigger buffer.  This
two-call approach can still employ stack-based buffers as per 1 or 2
above.

Other C functions may be able to be called multiple times without
compromising their state or the integrity of the result.  In this
case, if the returned length shows the buffer is full and you're not
sure whether it's truncated, you may have to allocate a bigger buffer
and recall the function just in case.

Note: no heap allocation.

Tony

-- 
      [ 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 Tue Jul 8 23:30:45 CDT 2008.