Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Indexing an auto_ptr'ed chunk of memory

Reply
Thread Tools

Indexing an auto_ptr'ed chunk of memory

 
 
Azdo
Guest
Posts: n/a
 
      08-18-2005
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,

 
Reply With Quote
 
 
 
 
Kyle
Guest
Posts: n/a
 
      08-18-2005
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,
>

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-18-2005
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
 
Reply With Quote
 
vindhya
Guest
Posts: n/a
 
      08-18-2005

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.

 
Reply With Quote
 
Azdo
Guest
Posts: n/a
 
      08-18-2005
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!
 
Reply With Quote
 
Serge Skorokhodov (216716244)
Guest
Posts: n/a
 
      08-18-2005
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
 
Reply With Quote
 
Alipha
Guest
Posts: n/a
 
      08-18-2005

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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sending a file chunk by chunk instead as a whole to a web server Sanjeeb Python 3 08-03-2010 05:52 AM
Indexing services under Windows XP SP2 - Can I disable MS Indexing Service to hasten Google's OR does Google Desktop uses this MS Indexing Service? ricardodefaria Computer Support 6 08-05-2007 04:14 AM
How much slower is dict indexing vs. list indexing? Emin Python 4 01-12-2007 02:40 PM
What is an AVI Chunk Viewer? - AVI Chunk Viewer.jpg (0/1) mazdra76@yahooo.com Computer Support 1 03-17-2006 02:52 AM
Indexing PDF Files using MS Indexing Service C ASP .Net 0 10-17-2003 04:27 PM



Advertisments