auto_ptr to char[]

T

Tim H

I have need of a temporary local array (yes it must be an array in
order to pass down to a C API).

I want to do this:

auto_ptr<char> my_data(new char[size]);
memcpy(my_data.get(), data, size);


Is this safe? I know you should not hold auto_ptr to arrays, but is
it OK if it is an array of primitive types?

If not, what;s the alternative?

Tim
 
J

Jim Langston

Tim said:
I have need of a temporary local array (yes it must be an array in
order to pass down to a C API).

I want to do this:

auto_ptr<char> my_data(new char[size]);
memcpy(my_data.get(), data, size);


Is this safe? I know you should not hold auto_ptr to arrays, but is
it OK if it is an array of primitive types?

If not, what;s the alternative?

An alternative is using a std::vector A std::vector's data is guaranteed to
be contiguous.

std::vector<char> my_data( size );
memcpy( &my_data[0], data, size );
 
T

Triple-DES

I have need of a temporary local array (yes it must be an array in
order to pass down to a C API).

I want to do this:

auto_ptr<char> my_data(new char[size]);
memcpy(my_data.get(), data, size);

Is this safe?  I know you should not hold auto_ptr to arrays, but is
it OK if it is an array of primitive types?

It's not safe because ~auto_ptr() will call delete, not delete[] on
your pointer. This is undefined behavior.
If not, what;s the alternative?

Use a vector like Jim L. said.
 
T

Torsten Mueller

Triple-DES said:
Use a vector like Jim L. said.

But what's bad on new/delete[]?
Or what's bad on using local variables on the stack?
Or why not use string-classes?

T.M.
 
T

Triple-DES

But what's bad on new/delete[]?

It's not a good thing in the presence of exceptions. You would have to
make try/catch blocks and put delete[] many places in your code to
make sure the resources are released correctly in all possible
execution paths. It's easy to get things wrong.
Or what's bad on using local variables on the stack?

I believe that the size parameter is not known at compile-time in this
case.
Or why not use string-classes?

Because the string's data is not guaranteed to be stored contiguously,
making the call to c_str necessary.
 
K

kasthurirangan.balaji

Tim said:
I have need of a temporary local array (yes it must be an array in
order to pass down to a C API).
I want to do this:
auto_ptr<char> my_data(new char[size]);
memcpy(my_data.get(), data, size);
Is this safe?  I know you should not hold auto_ptr to arrays, but is
it OK if it is an array of primitive types?
If not, what;s the alternative?

An alternative is using a std::vector  A std::vector's data is guaranteed to
be contiguous.

std::vector<char> my_data( size  );
memcpy( &my_data[0], data, size );

better would be using std::fill rather than memcpy.

Thanks,
Balaji.
 
K

kasthurirangan.balaji

Tim said:
I have need of a temporary local array (yes it must be an array in
order to pass down to a C API).
I want to do this:
auto_ptr<char> my_data(new char[size]);
memcpy(my_data.get(), data, size);
Is this safe?  I know you should not hold auto_ptr to arrays, but is
it OK if it is an array of primitive types?
If not, what;s the alternative?
An alternative is using a std::vector  A std::vector's data is guaranteed to
be contiguous.
std::vector<char> my_data( size  );
memcpy( &my_data[0], data, size );

better would be using std::fill rather than memcpy.

Apologize. thought it was memset(though typed memcpy correctly :) ),
hence suggested std::fill.

Thanks,
Balaji.
 
J

James Kanze

But what's bad on new/delete[]?
It's not a good thing in the presence of exceptions. You would
have to make try/catch blocks and put delete[] many places in
your code to make sure the resources are released correctly in
all possible execution paths. It's easy to get things wrong.

Well, if the function is just a wrapper to a C function, there's
not much chance that the C function will throw an exception:).
(But std::vector is still simpler and more reliable.)
I believe that the size parameter is not known at compile-time
in this case.
Because the string's data is not guaranteed to be stored
contiguously, making the call to c_str necessary.

That's true in the current version of the standard, but in fact,
all existing implementations of std::string do store data
contiguously, and the next version of the standard will
guarantee this, so you're OK anyway.

Still, all other things being equal, I'd use std::vector<char>.
It seems to express the intent (an array) best.
 
D

Dennis Jones

Tim H said:
I have need of a temporary local array (yes it must be an array in
order to pass down to a C API).

I want to do this:

auto_ptr<char> my_data(new char[size]);
memcpy(my_data.get(), data, size);


Is this safe?

No, because the destructor will do:

delete ptr

and you need:

delete [] ptr

I know you should not hold auto_ptr to arrays, but is
it OK if it is an array of primitive types?

If not, what;s the alternative?

Look at boost::scoped_array.

- Dennis
 
H

Howard Hinnant

"Dennis Jones said:
Tim H said:
I have need of a temporary local array (yes it must be an array in
order to pass down to a C API).

I want to do this:

auto_ptr<char> my_data(new char[size]);
memcpy(my_data.get(), data, size);


Is this safe?

No, because the destructor will do:

delete ptr

and you need:

delete [] ptr

I know you should not hold auto_ptr to arrays, but is
it OK if it is an array of primitive types?

If not, what;s the alternative?

Look at boost::scoped_array.

- Dennis

Fwiw, the current C++0X working paper
(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2521.pdf) has
wording that will allow:

std::unique_ptr<char[]> my_data(new char[size]);
memcpy(my_data.get(), data, size);

Here is the reference implementation:

http://home.twcny.rr.com/hinnant/cpp_extensions/unique_ptr.html

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top