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
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
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
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.
auto_ptr<char> buffer(new char[size]);
the ctor is explicit to force to write it.
> 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.
> thanks,
> marc
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
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
rhan...@pobox.com
Russell Hanneken wrote:
>
> "tf" <a...@abc.com> wrote in message
> news:bf5k51$b7hjj$1...@ID-57289.news.uni-berlin.de...
> > Marc Schellens wrote:
> > > 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.
>
> 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.
--
Karl Heinz Buchegger
kbuc...@gascad.at
No, but you don't need it.
std::vector<char> buffer (size); will give you an array with
the same layout. &buffer[0]+5=&buffer[5] etc.
Regards,
--
Michiel Salters
The point is, that I have to do istream.read(char* buf,size_t)
writing is no problem as I can use string::c_str(),
but there is no way to get a ptr to the data area (the char[])
of a string for overwriting, as many implementations just don't use one.
marc