auto_ptr for arrays? 
Author Message
 auto_ptr for arrays?

Hello

Is there a template class that works like auto_ptr but can take arrays so
you can write
{
    auto_ptr?<char> str(new char[length]);

Quote:
}

and the pointer would be automatically released by operator [] ?

Thanks in advance
Peter Str?iman



Tue, 18 May 2004 23:44:38 GMT  
 auto_ptr for arrays?

Quote:
> Hello

> Is there a template class that works like auto_ptr but can take arrays so
> you can write
> {
>     auto_ptr?<char> str(new char[length]);

> }
> and the pointer would be automatically released by operator [] ?

Boost has one called auto_array_ptr, I believe.  Otherwise, is there some
reason you can't use std::vector?

Sean



Wed, 19 May 2004 03:45:49 GMT  
 auto_ptr for arrays?
Is there one? I have no idea. I'm responding anyway to say that Meyer's
"Effective STL" book warns:
"Never create containers of auto_ptrs." Meyers spends 3 pages saying that
Containers of Auto_ptrs (COAPs) are against the C++ Standardization
Committee, and should never compile. Current compilers often let such things
through, though he expects that to change in the future. If you want to read
Item 8 in this book yourself, the ISBN is 0-201-74962-9. I have no
connection to Meyers, other than thinking "Effective STL" was worth the
$39.95 US dollars I paid for it.

Randy Bone


Quote:
> Hello

> Is there a template class that works like auto_ptr but can take arrays so
> you can write
> {
>     auto_ptr?<char> str(new char[length]);

> }
> and the pointer would be automatically released by operator [] ?

> Thanks in advance
> Peter Str?iman



Wed, 19 May 2004 03:51:43 GMT  
 auto_ptr for arrays?


Quote:
> Is there one? I have no idea. I'm responding anyway to say that Meyer's
> "Effective STL" book warns:
> "Never create containers of auto_ptrs."

Yes but he is doing the opposite.

He is not wanting an array of auto_ptrs
He is not wanting an auto_ptr of an array

Big difference

Stephen Howe



Wed, 19 May 2004 04:52:48 GMT  
 auto_ptr for arrays?

Sorry that should have been

He is not wanting an array of auto_ptrs
He is wanting an auto_ptr of an array

Stephen Howe



Sat, 22 May 2004 03:29:34 GMT  
 auto_ptr for arrays?
std::vector - now why didn't I think of that.

if my code was previously
{
char *Str
str = new char[length+1];
memcpy(Str, pointer_to_a_string_without_a_terminating_0, length);
Str[length] = 0;
// use the string
delete [] Str;

Quote:
}

how would I use the vector?
{
vector<char> Str;
Str.reserve(length+1);
memcpy(Str.begin(), pointer..., length);
Str[length] = 0;
Quote:
}

?

Thanks,
Peter Str?iman

Quote:


> > Hello

> > Is there a template class that works like auto_ptr but can take arrays
so
> > you can write
> > {
> >     auto_ptr?<char> str(new char[length]);

> > }
> > and the pointer would be automatically released by operator [] ?

> Boost has one called auto_array_ptr, I believe.  Otherwise, is there some
> reason you can't use std::vector?

> Sean



Sat, 22 May 2004 19:44:19 GMT  
 auto_ptr for arrays?

Quote:
>Str.reserve(length+1);

This reserves memory but does not resize the vector. Your vector is still 0
size and so the following memcpy() is illegal.

Quote:
> memcpy(Str.begin(), pointer..., length);

No. Str.begin() returns an iterator, not a pointer. Use  &Str[0] instead

Quote:
> Str[length] = 0;

Try instead

vector<char> Str(length+1);
memcpy(&Str[0], pointer..., length);
Str[length] = 0;

Stephen Howe



Sat, 22 May 2004 22:06:24 GMT  
 auto_ptr for arrays?
Hi!

Why don't you just use vector's constructor that
supports iterators:

char buffer[] = {...};
vector<char> Str(buffer, (sizeof(buffer) /
sizeof(char)));

This should copy the array into the vector, and
the iterators expected are forward iterators.
Ordinary C pointers are equal to random access
iterators.

Best regards
Stefan

"Stephen Howe"


Quote:
> >Str.reserve(length+1);

> This reserves memory but does not resize the

vector. Your vector is still 0
Quote:
> size and so the following memcpy() is illegal.

> > memcpy(Str.begin(), pointer..., length);

> No. Str.begin() returns an iterator, not a

pointer. Use  &Str[0] instead
Quote:

> > Str[length] = 0;

> Try instead

> vector<char> Str(length+1);
> memcpy(&Str[0], pointer..., length);
> Str[length] = 0;

> Stephen Howe



Sat, 22 May 2004 23:56:21 GMT  
 auto_ptr for arrays?
That should be

vector<char> Str(buffer, buffer + sizeof(buffer));

Both arguments are pointers. sizeof(char) == 1 by definition.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat, and
wrong." H.L. Mencken


Quote:
> Hi!

> Why don't you just use vector's constructor that
> supports iterators:

> char buffer[] = {...};
> vector<char> Str(buffer, (sizeof(buffer) /
> sizeof(char)));

> This should copy the array into the vector, and
> the iterators expected are forward iterators.
> Ordinary C pointers are equal to random access
> iterators.

> Best regards
> Stefan

> "Stephen Howe"


> > >Str.reserve(length+1);

> > This reserves memory but does not resize the
> vector. Your vector is still 0
> > size and so the following memcpy() is illegal.

> > > memcpy(Str.begin(), pointer..., length);

> > No. Str.begin() returns an iterator, not a
> pointer. Use  &Str[0] instead

> > > Str[length] = 0;

> > Try instead

> > vector<char> Str(length+1);
> > memcpy(&Str[0], pointer..., length);
> > Str[length] = 0;

> > Stephen Howe



Sun, 23 May 2004 00:01:45 GMT  
 auto_ptr for arrays?
Oops, you're right. Sorry... I was too thinking
too fast.



Quote:
> That should be

> vector<char> Str(buffer, buffer +
sizeof(buffer));

> Both arguments are pointers. sizeof(char) == 1
by definition.
> --
> With best wishes,
>     Igor Tandetnik

> "For every complex problem, there is a solution

that is simple, neat, and
Quote:
> wrong." H.L. Mencken


message

> > Hi!

> > Why don't you just use vector's constructor
that
> > supports iterators:

> > char buffer[] = {...};
> > vector<char> Str(buffer, (sizeof(buffer) /
> > sizeof(char)));

> > This should copy the array into the vector,
and
> > the iterators expected are forward iterators.
> > Ordinary C pointers are equal to random access
> > iterators.

> > Best regards
> > Stefan

> > "Stephen Howe"

im
> > Newsbeitrag


- Show quoted text -

Quote:
> > > >Str.reserve(length+1);

> > > This reserves memory but does not resize the
> > vector. Your vector is still 0
> > > size and so the following memcpy() is
illegal.

> > > > memcpy(Str.begin(), pointer..., length);

> > > No. Str.begin() returns an iterator, not a
> > pointer. Use  &Str[0] instead

> > > > Str[length] = 0;

> > > Try instead

> > > vector<char> Str(length+1);
> > > memcpy(&Str[0], pointer..., length);
> > > Str[length] = 0;

> > > Stephen Howe



Sun, 23 May 2004 00:13:14 GMT  
 auto_ptr for arrays?
How about using std::string?

Dan


Quote:
> Hello

> Is there a template class that works like auto_ptr but can take arrays so
> you can write
> {
>     auto_ptr?<char> str(new char[length]);

> }
> and the pointer would be automatically released by operator [] ?

> Thanks in advance
> Peter Str?iman



Sun, 23 May 2004 13:08:57 GMT  
 auto_ptr for arrays?
On Fri, 30 Nov 2001 16:44:38 +0100, "Peter Str?iman" <peter_ps-consult.dk>

Quote:
>Hello

>Is there a template class that works like auto_ptr but can take arrays so
>you can write
>{
>    auto_ptr?<char> str(new char[length]);

>}
>and the pointer would be automatically released by operator [] ?

Here's one I wrote if you care to try it:
===============================
#if !defined(FF808862_CC3E_4C6A_BB18_B37299796E2F)
#define       FF808862_CC3E_4C6A_BB18_B37299796E2F

namespace autoarray {

        template<class T> class autoarray_ptr {
        protected:
                T  *t; // T array ptr holder
                T  *r; // for release() to give up ownership

                size_t array_size; // so users can set and get array size

        public:

                explicit autoarray_ptr(T *p = 0) throw() : t(p), array_size(0)
{}

                explicit autoarray_ptr(T *p, size_t size) throw() : t(p)
                {
                        array_size = (t) ? size : 0;
                }

                autoarray_ptr(autoarray_ptr<T>& rhs) throw()
                {
                        if(this != &rhs) {
                                array_size = rhs.getsize();
                                t = rhs.release();
                        }
                }

                autoarray_ptr<T>& operator=(autoarray_ptr<T>& rhs) throw()
                {
                        if(this != &rhs) {
                                array_size = rhs.getsize();
                                t = rhs.release();
                        }
                        return (*this);
                }

                ~autoarray_ptr() { delete [] t; }

                size_t  getsize() throw() { return array_size; }
                void setsize(size_t size) throw() { array_size = size; }

                T& operator[](size_t i) throw() { return t[i]; }
                T *get() throw() { return t; }

                T *release() throw()
                {
                        r = t;
                        t = 0;
                        array_size = 0;
                        return r;
                }

                void reset(T *p = 0) throw()
                {
                        if(t != p) {
                                delete [] t;
                                t = p;
                                if(!t) array_size = 0;
                        }
                }

                void reset(T *p, size_t size) throw()
                {
                        if(t != p) {
                                delete [] t;
                                t = p;
                                array_size = (t) ? size : 0;
                        }
                }
        };

Quote:
}

#endif

Mike

--

"Genius gives birth, talent delivers."

           -- Jack Kerouac

(Remove NOSPAM, if present, to reply via email)



Mon, 24 May 2004 09:50:20 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. auto_ptr for arrays

2. auto_ptr for arrays

3. auto_ptr for arrays

4. arrays in auto_ptr

5. MS auto_ptr vs. ANSI auto_ptr.

6. auto_ptr assignment broken in 7.1?

7. Problem with auto_ptr

8. auto_ptr problems

9. auto_ptr & C2440

10. auto_ptr in STL collections

11. bug: VS7.0 (6.0) C++ std::auto_ptr conflict with std::vector

12. Auto_ptr ownership

 

 
Powered by phpBB® Forum Software