Indexing an auto_ptr'ed chunk of memory

A

Azdo

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,
 
K

Kyle

Azdo said:
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,
 
K

Kai-Uwe Bux

Azdo said:
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
 
V

vindhya

Kyle said:
Azdo said:
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.
 
A

Azdo

vindhya said:
Azdo said:
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!
 
A

Alipha

Azdo said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top