473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

auto_ptr to char[ size_t]?

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

Jul 19 '05 #1
8 5710
tf
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
Jul 19 '05 #2

"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@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
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
Jul 19 '05 #3
>>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.

Jul 19 '05 #4
>>>>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

Jul 19 '05 #5
Marc Schellens <m_*********@hotmail.com> wrote in message news:<3F**************@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

Jul 19 '05 #6

"DarkSpy" <co****@21cn.com> wrote in message
news:aa**************************@posting.google.c om...
Marc Schellens <m_*********@hotmail.com> wrote in message

news:<3F**************@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.


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
Jul 19 '05 #7
"tf" <ab*@abc.com> wrote in message
news:bf************@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.

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
rh*******@pobox.com
Jul 19 '05 #8


Russell Hanneken wrote:

"tf" <ab*@abc.com> wrote in message
news:bf************@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
kb******@gascad.at
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
2
by: Morgan Cheng | last post by:
Below code has output Foo created 3 0 doit Foo destructed 3 I am confused. When an auto_ptr object is assigned to another auto_ptr object, the control of pointer is transferred from orginal...
5
by: gg | last post by:
I am getting the following compilation errors with the following program. My compiler is aCC 03.27 on HP-UX11 - #include <iostream> using namespace std; #include <list> #include <memory>...
43
by: M-One | last post by:
See subject: how do I calloc (and free the memory, if that's not free(my_bytes);) this? TIA!
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
4
by: Vijay Bajwa | last post by:
Say I have a const char* msg which is pointing to memory gotten by something like (msg = new char ). Then if I declare thus: auto_ptr<const charsmartptr (msg) ; Then will this bomb royally,...
4
by: james.lawton | last post by:
Hi, I'm having a problem that I can't diagnose. I'm creating istreams of unknown types, and want to manage them on the stack, co I'm passing around ownership-holding pointers. Usually, I would...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.