| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Tim H" |
| Date: |
31 Jan 2008 11:33:02 PM |
| Object: |
auto_ptr to char[] |
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
.
|
|
| User: "Triple-DES" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 01:08:47 AM |
|
|
On 1 Feb, 06:33, Tim H <thoc...@gmail.com> wrote:
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? =A0I 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.
.
|
|
|
| User: "Torsten Mueller" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 01:51:21 AM |
|
|
Triple-DES <fire13all@hotmail.com> schrieb:
If not, what;s the alternative?
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.
.
|
|
|
| User: "Triple-DES" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 03:42:57 AM |
|
|
On 1 Feb, 08:51, Torsten Mueller <dev-n...@shared-files.de> wrote:
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.
.
|
|
|
| User: "James Kanze" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 07:18:30 AM |
|
|
On Feb 1, 10:42 am, Triple-DES <fire13...@hotmail.com> wrote:
On 1 Feb, 08:51, Torsten Mueller <dev-n...@shared-files.de> wrote:
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.)
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.
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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
|
|
|
| User: "Jim Langston" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 12:30:03 AM |
|
|
Tim H wrote:
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 );
--
Jim Langston
tazmaster@rocketmail.com
.
|
|
|
| User: "" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 06:13:28 AM |
|
|
On Feb 1, 11:30=A0am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
Tim H wrote:
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? =A0I 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 =A0A std::vector's data is guarantee=
d to
be contiguous.
std::vector<char> my_data( size =A0);
memcpy( &my_data[0], data, size );
better would be using std::fill rather than memcpy.
Thanks,
Balaji.
--
Jim Langston
tazmas...@rocketmail.com
.
|
|
|
| User: "" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 06:19:05 AM |
|
|
On Feb 1, 5:13=A0pm, wrote:
On Feb 1, 11:30=A0am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
Tim H wrote:
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? =A0I 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 =A0A std::vector's data is guarant=
eed to
be contiguous.
std::vector<char> my_data( size =A0);
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.
Thanks,
Balaji.
--
Jim Langston
tazmas...@rocketmail.com- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
.
|
|
|
| User: "Triple-DES" |
|
| Title: Re: auto_ptr to char[] |
01 Feb 2008 06:43:29 AM |
|
|
On 1 Feb, 13:19, wrote:
On Feb 1, 11:30=A0am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
std::vector<char> my_data( size =A0);
memcpy( &my_data[0], data, size );
better would be using std::fill rather than memcpy.
In this case I think the best way to put data into the vector is
simply
std::vector<char> my_data(data, data + size);
.
|
|
|
|
|
|
|

|
Related Articles |
|
|