Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

auto_ptr to char[ size_t]?

144 views
Skip to first unread message

Marc Schellens

unread,
Jul 16, 2003, 9:54:00 AM7/16/03
to
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

tf

unread,
Jul 17, 2003, 2:43:55 AM7/17/03
to
Marc Schellens wrote:
> 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


John Harrison

unread,
Jul 17, 2003, 3:21:30 AM7/17/03
to

"Marc Schellens" <m_sch...@hotmail.com> wrote in message
news:3F1558F8...@hotmail.com...

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


Marc Schellens

unread,
Jul 16, 2003, 2:27:17 PM7/16/03
to

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

tf

unread,
Jul 17, 2003, 3:44:31 AM7/17/03
to

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.


Marc Schellens

unread,
Jul 16, 2003, 2:43:33 PM7/16/03
to
Ok, I will do that.
Thanks,
marc

DarkSpy

unread,
Jul 17, 2003, 4:49:37 AM7/17/03
to
Marc Schellens <m_sch...@hotmail.com> wrote in message news:<3F1558F8...@hotmail.com>...
> 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

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

John Harrison

unread,
Jul 17, 2003, 4:57:14 AM7/17/03
to

"DarkSpy" <con...@21cn.com> wrote in message
news:aacb0456.03071...@posting.google.com...

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


Russell Hanneken

unread,
Jul 17, 2003, 5:04:00 AM7/17/03
to
"tf" <a...@abc.com> wrote in message
news:bf5k51$b7hjj$1...@ID-57289.news.uni-berlin.de...

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


Karl Heinz Buchegger

unread,
Jul 17, 2003, 5:11:35 AM7/17/03
to

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

Michiel Salters

unread,
Jul 17, 2003, 5:15:08 AM7/17/03
to
Marc Schellens <m_sch...@hotmail.com> wrote in message news:<3F1558F8...@hotmail.com>...

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

Marc Schellens

unread,
Jul 17, 2003, 6:13:44 AM7/17/03
to

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

0 new messages