auto_ptr to char[ size_t]?

M

Marc Schellens

I want to use an auto_ptr to a buffer:

auto_ptr<char> buffer = new char[ size];

But this does not work, as auto_ptr calls
delete instead of delete[], right?

What I can do? Is there a version of auto_ptr calling delete[]?

thanks,
marc
 
M

Marc Schellens

I want to use an auto_ptr to a buffer:
auto_ptr<char> buffer = new char[ size];

But this does not work, as auto_ptr calls
delete instead of delete[], right?

Right


What I can do? Is there a version of auto_ptr calling delete[]?

thanks,
marc


Use std::vector or std::string instead.
If you insist on using a smart pointer look
into the boost smart pointers www.boost.org

I want to do ostream.write(char*,count) for the string
(unformatted), therefore vector or string are no options here.
 
M

Marc Schellens

I want to use an auto_ptr to a buffer:
auto_ptr<char> buffer = new char[ size];

But this does not work, as auto_ptr calls
delete instead of delete[], right?


Right



What I can do? Is there a version of auto_ptr calling delete[]?

thanks,
marc


Use std::vector or std::string instead.
If you insist on using a smart pointer look
into the boost smart pointers www.boost.org

I want to do ostream.write(char*,count) for the string
(unformatted), therefore vector or string are no options here.


Why is a std::vector<char> no option?
You can provide its constructor the initial size,
and use the pointer to its first element as the parameter
in your function call.
Ok, I will do that.
Thanks,
marc
 
T

tf

Marc said:
I want to use an auto_ptr to a buffer:

auto_ptr<char> buffer = new char[ size];

But this does not work, as auto_ptr calls
delete instead of delete[], right?
Right

What I can do? Is there a version of auto_ptr calling delete[]?

thanks,
marc

Use std::vector or std::string instead.
If you insist on using a smart pointer look
into the boost smart pointers www.boost.org
 
J

John Harrison

Marc Schellens said:
I want to use an auto_ptr to a buffer:

auto_ptr<char> buffer = new char[ size];

But this does not work, as auto_ptr calls
delete instead of delete[], right?

What I can do? Is there a version of auto_ptr calling delete[]?

thanks,
marc

You could go to your auto_ptr header (its in <memory> if memory serves!),
cut and paste the code into your editor (don't forget the copyright
message). Rename the class, and replace delete with delete[].

john
 
D

DarkSpy

Marc Schellens said:
I want to use an auto_ptr to a buffer:

auto_ptr<char> buffer = new char[ size];
But this does not work, as auto_ptr calls

delete instead of delete[], right?

What I can do? Is there a version of auto_ptr calling delete[]?
delete included in dtor of auto_ptr.
 
J

John Harrison

DarkSpy said:
Marc Schellens <[email protected]> wrote in message
I want to use an auto_ptr to a buffer:

auto_ptr<char> buffer = new char[ size];
But this does not work, as auto_ptr calls

delete instead of delete[], right?

What I can do? Is there a version of auto_ptr calling delete[]?
delete included in dtor of auto_ptr.

Yes but the point is that using delete on a pointer that you created with
new[] invokes undefined behaviour. auto_ptr cannot be used with arrays.

john
 
R

Russell Hanneken

tf said:
Why is a std::vector<char> no option?
You can provide its constructor the initial size,
and use the pointer to its first element as the parameter
in your function call.

If I recall correctly, there's no guarantee that std::vector stores its
elements in an array. So in theory, that solution might not work. I admit
that, realistically, every implementation of std::vector probably does use
an array.

In any case, wouldn't the more natural solution be to store the characters
in a std::string, and then invoke std::string's data() member function to
get a character array when it's needed?

Regards,

Russell Hanneken
(e-mail address removed)
 
K

Karl Heinz Buchegger

Russell said:
If I recall correctly, there's no guarantee that std::vector stores its
elements in an array. So in theory, that solution might not work. I admit
that, realistically, every implementation of std::vector probably does use
an array.

The concensus is this:

* There is no guarantee
* This has probably been an oversight while comming up with the standard
* The next version of the standard will guarantee this
* It is hard or impossible to fullfill the requirements of std::vector
if the data is not stored contigous
* There is no known version which does not store the data contigous.
In any case, wouldn't the more natural solution be to store the characters
in a std::string, and then invoke std::string's data() member function to
get a character array when it's needed?

Could be. But a character pointer is often used to denote simply
a sequence of bytes. Not necessarly text.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top