473,846 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Indexing an auto_ptr'ed chunk of memory

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<i nt> 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<i nt> 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,

Aug 18 '05 #1
6 1524
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<i nt> 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<i nt> 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,

Aug 18 '05 #2

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<i nt> 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<i nt> 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.

Aug 18 '05 #3
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<i nt> 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<i nt> 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
Aug 18 '05 #4
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<i nt> 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<i nt> 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!
Aug 18 '05 #5
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_pt r lib at www.boost.org

--
Serge
Aug 18 '05 #6

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<i nt> 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<i nt> 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<s td::vector<int> > however, if that's for
"optimizati on" purposes, you're probably better off just avoiding
copying the container (eg, pass by reference).

Aug 18 '05 #7

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

Similar topics

5
4085
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> #include <string>
4
3554
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
13
2094
by: Shark | last post by:
Hi, I was reading on http://www.kuro5hin.org/story/2005/10/31/53857/831 that auto_ptr uses "delete" in its destructor instead of delete. The article is fairly recent, so I was wondering if this is true in general for any hosted implementation.
23
1574
by: Jeff | last post by:
After reading all I could find about auto_ptr, I decided to write a little program to test my comprehension: #include <iostream> #include <memory> class A { public: void say_hi() { std::cout << "hi!" << std::endl;
10
2640
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
9
2923
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the behaviour of std::auto_ptr. 2. To implement a pkt::auto_ptr without auto_ptr_ref. 3. Check out the limitations of pkt::auto_ptr as compared to std::auto_ptr (using steps 1 and 2).
39
864
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 Query("123");
17
3086
by: christophe.chazeau | last post by:
Hi, I have a problem with a really simple chunk of code which should work but does obviously does not. This chunk of code is just a POC aimed at finding a bug in a larger project in which the same problem seems to occur. Here the deal : when I run this piece of code, I expect all the memory allocated by the "Test" object to be freed but what I observe is that after the second sleep (after all the additions to the vector), the memory...
0
9879
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9725
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10976
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7877
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5714
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5906
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.