Indexing an auto_ptr'ed chunk of memory

Discussion in 'C++' started by Azdo, Aug 18, 2005.

  1. Azdo

    Azdo Guest

    Hello,

    if i wanted to access the elements of an array which is owned by an
    auto_ptr, as:

    #include <memory>

    int main(){
    std::auto_ptr<int> p(new int[10]);
    p[2]=2; //Error: no operator[] defined
    return 0;
    }

    would the best way be:

    #include <memory>

    int main(){
    std::auto_ptr<int> p(new int[10]);
    int *p_;
    p_=p.get();
    p_[2]=2;

    return 0;
    }

    I thought deriving a class from auto_ptr, but the pointer member is
    protected and subject to have different names on different C++ standard
    library implementations.

    TIA,
     
    Azdo, Aug 18, 2005
    #1
    1. Advertising

  2. Azdo

    Kyle Guest

    Azdo wrote:
    > Hello,
    >
    > if i wanted to access the elements of an array which is owned by an
    > auto_ptr, as:


    you should not use std::auto_ptr<> for arrays as in its destuctor it
    'delete' owned pointer (and you dont want it to 'delete' memory that was
    acquired with 'new[]')

    >
    > #include <memory>
    >
    > int main(){
    > std::auto_ptr<int> p(new int[10]);
    > p[2]=2; //Error: no operator[] defined
    > return 0;
    > }
    >
    > would the best way be:
    >
    > #include <memory>
    >
    > int main(){
    > std::auto_ptr<int> p(new int[10]);
    > int *p_;
    > p_=p.get();
    > p_[2]=2;
    >
    > return 0;
    > }
    >
    > I thought deriving a class from auto_ptr, but the pointer member is
    > protected and subject to have different names on different C++ standard
    > library implementations.
    >
    > TIA,
    >
     
    Kyle, Aug 18, 2005
    #2
    1. Advertising

  3. Azdo

    Kai-Uwe Bux Guest

    Azdo wrote:

    > Hello,
    >
    > if i wanted to access the elements of an array which is owned by an
    > auto_ptr, as:
    >
    > #include <memory>
    >
    > int main(){
    > std::auto_ptr<int> p(new int[10]);
    > p[2]=2; //Error: no operator[] defined
    > return 0;
    > }
    >
    > would the best way be:
    >
    > #include <memory>
    >
    > int main(){
    > std::auto_ptr<int> p(new int[10]);
    > int *p_;
    > p_=p.get();
    > p_[2]=2;
    >
    > return 0;
    > }
    >
    > I thought deriving a class from auto_ptr, but the pointer member is
    > protected and subject to have different names on different C++ standard
    > library implementations.
    >
    > TIA,


    Hm, I doubt that it is a good idea to use std::auto_ptr to hold an array:
    Somewhere inside the auto_ptr object there ought to be a pointer int* ptr.
    At initialisation, you set ptr = new int [10]. However, upon destruction of
    the auto_ptr object, memory shall be released, and I bet you, the
    destructor will say: delete ptr. Note that it does not say: delete[] ptr.
    Thus, in your code, new[] and delete will not match.

    I think, you could define your own auto_array_ptr code quickly, or look for
    some library. I would guess this has been done before. Such an
    auto_array_ptr class would define operator[].


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 18, 2005
    #3
  4. Azdo

    vindhya Guest

    Kyle wrote:
    > Azdo wrote:
    > > Hello,
    > >
    > > if i wanted to access the elements of an array which is owned by an
    > > auto_ptr, as:

    >
    > you should not use std::auto_ptr<> for arrays as in its destuctor it
    > 'delete' owned pointer (and you dont want it to 'delete' memory that was
    > acquired with 'new[]')
    >
    > >
    > > #include <memory>
    > >
    > > int main(){
    > > std::auto_ptr<int> p(new int[10]);
    > > p[2]=2; //Error: no operator[] defined
    > > return 0;
    > > }
    > >
    > > would the best way be:
    > >
    > > #include <memory>
    > >
    > > int main(){
    > > std::auto_ptr<int> p(new int[10]);
    > > int *p_;
    > > p_=p.get();
    > > p_[2]=2;
    > >
    > > return 0;
    > > }
    > >
    > > I thought deriving a class from auto_ptr, but the pointer member is
    > > protected and subject to have different names on different C++ standard
    > > library implementations.
    > >
    > > TIA,
    > >


    I think you can use auto_ptr on individual elements of array.
     
    vindhya, Aug 18, 2005
    #4
  5. Azdo

    Azdo Guest

    vindhya wrote:

    >
    > Kyle wrote:
    >> Azdo wrote:
    >> > Hello,
    >> >
    >> > if i wanted to access the elements of an array which is owned by an
    >> > auto_ptr, as:

    >>
    >> you should not use std::auto_ptr<> for arrays as in its destuctor it
    >> 'delete' owned pointer (and you dont want it to 'delete' memory that was
    >> acquired with 'new[]')
    >>
    >> >
    >> > #include <memory>
    >> >
    >> > int main(){
    >> > std::auto_ptr<int> p(new int[10]);
    >> > p[2]=2; //Error: no operator[] defined
    >> > return 0;
    >> > }
    >> >
    >> > would the best way be:
    >> >
    >> > #include <memory>
    >> >
    >> > int main(){
    >> > std::auto_ptr<int> p(new int[10]);
    >> > int *p_;
    >> > p_=p.get();
    >> > p_[2]=2;
    >> >
    >> > return 0;
    >> > }
    >> >
    >> > I thought deriving a class from auto_ptr, but the pointer member is
    >> > protected and subject to have different names on different C++ standard
    >> > library implementations.
    >> >
    >> > TIA,
    >> >

    >
    > I think you can use auto_ptr on individual elements of array.


    Oh, I think I didn't notice the array issue at all! That's why the []
    operator is not defined...

    Thanks Kai, Kyle, vindhya!
     
    Azdo, Aug 18, 2005
    #5
  6. Hi,

    <skip>

    >> I think you can use auto_ptr on individual elements of
    >> array.

    >
    >
    > Oh, I think I didn't notice the array issue at all! That's why
    > the [] operator is not defined...
    >


    Try boost::smart_ptr lib at www.boost.org

    --
    Serge
     
    Serge Skorokhodov (216716244), Aug 18, 2005
    #6
  7. Azdo

    Alipha Guest

    Azdo wrote:
    > Hello,
    >
    > if i wanted to access the elements of an array which is owned by an
    > auto_ptr, as:
    >
    > #include <memory>
    >
    > int main(){
    > std::auto_ptr<int> p(new int[10]);
    > p[2]=2; //Error: no operator[] defined
    > return 0;
    > }
    >
    > would the best way be:
    >
    > #include <memory>
    >
    > int main(){
    > std::auto_ptr<int> p(new int[10]);
    > int *p_;
    > p_=p.get();
    > p_[2]=2;
    >
    > return 0;
    > }
    >
    > I thought deriving a class from auto_ptr, but the pointer member is
    > protected and subject to have different names on different C++ standard
    > library implementations.
    >
    > TIA,


    use a std::vector or another container for collections of values.
    possibly use std::auto_ptr<std::vector<int> > however, if that's for
    "optimization" purposes, you're probably better off just avoiding
    copying the container (eg, pass by reference).
     
    Alipha, Aug 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C
    Replies:
    0
    Views:
    519
  2. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,575
    Dave Rahardja
    Jan 11, 2005
  3. Emin
    Replies:
    4
    Views:
    431
    Paul McGuire
    Jan 12, 2007
  4. Sousuke
    Replies:
    9
    Views:
    1,175
    Bart van Ingen Schenau
    Mar 16, 2010
  5. Sanjeeb
    Replies:
    3
    Views:
    441
    Ryan Kelly
    Aug 3, 2010
Loading...

Share This Page