Connecting Tech Pros Worldwide Help | Site Map

auto_ptr

bb
Guest
 
Posts: n/a
#1: Oct 11 '06
Hi,

Is the following safe? (it works fine and to me it looks exception
safe!).

void thirdPartyLibFun(T* t) {
...
}

void myFun() {
std::auto_ptr<Tap2t = std::auto_ptr<T>(new T);

thirdPartyLibFun(ap2t.get());
}

Thanks.

Ron Natalie
Guest
 
Posts: n/a
#2: Oct 11 '06

re: auto_ptr


bb wrote:
Quote:
Hi,
>
Is the following safe? (it works fine and to me it looks exception
safe!).
>
void thirdPartyLibFun(T* t) {
...
}
>
void myFun() {
std::auto_ptr<Tap2t = std::auto_ptr<T>(new T);
Try direct initialization, lose the cast.
Quote:
>
thirdPartyLibFun(ap2t.get());
}
>
This isn't @*^&@! JAVA. If you want a local object, use a
local object.

void myFun() {
T x;
thirdPartyLibFun(&x);
}

Plenty exception safe and a bit more efficient.
Clark S. Cox III
Guest
 
Posts: n/a
#3: Oct 11 '06

re: auto_ptr


bb wrote:
Quote:
Hi,
>
Is the following safe? (it works fine and to me it looks exception
safe!).
>
void thirdPartyLibFun(T* t) {
...
}
>
void myFun() {
std::auto_ptr<Tap2t = std::auto_ptr<T>(new T);
>
thirdPartyLibFun(ap2t.get());
}
>
Thanks.
>
What's wrong with:

void myFun() {
std::auto_ptr<Tap2t(new T);
thirdPartyLibFun(ap2t.get());
}

Or (even better IMO):

void myFun() {
T t;
thirdPartyLibFun(&t);
}

--
Clark S. Cox III
clarkcox3@gmail.com
LR
Guest
 
Posts: n/a
#4: Oct 11 '06

re: auto_ptr


Ron Natalie wrote:
Quote:
bb wrote:
>
Quote:
>Hi,
>>
>Is the following safe? (it works fine and to me it looks exception
>safe!).
>>
> void thirdPartyLibFun(T* t) {
> ...
> }
>>
> void myFun() {
> std::auto_ptr<Tap2t = std::auto_ptr<T>(new T);
>
>
Try direct initialization, lose the cast.
>
Quote:
>>
> thirdPartyLibFun(ap2t.get());
> }
>
>
This isn't @*^&@! JAVA. If you want a local object, use a
local object.
>
void myFun() {
T x;
thirdPartyLibFun(&x);
}
>
Plenty exception safe and a bit more efficient.
Do you hae the souce code for thirdPartyLibFun(T *t)? What if it
deletes the T that is pointed to?

LR
Kai-Uwe Bux
Guest
 
Posts: n/a
#5: Oct 11 '06

re: auto_ptr


LR wrote:
Quote:
Ron Natalie wrote:
Quote:
>bb wrote:
>>
Quote:
>>Hi,
>>>
>>Is the following safe? (it works fine and to me it looks exception
>>safe!).
>>>
>> void thirdPartyLibFun(T* t) {
>> ...
>> }
>>>
>> void myFun() {
>> std::auto_ptr<Tap2t = std::auto_ptr<T>(new T);
>>
>>
>Try direct initialization, lose the cast.
>>
Quote:
>>>
>> thirdPartyLibFun(ap2t.get());
>> }
>>
>>
>This isn't @*^&@! JAVA. If you want a local object, use a
>local object.
>>
> void myFun() {
> T x;
> thirdPartyLibFun(&x);
> }
>>
>Plenty exception safe and a bit more efficient.
>
Do you hae the souce code for thirdPartyLibFun(T *t)? What if it
deletes the T that is pointed to?
In that case, the original code with auto_ptr would note fare any better:
instead of deleting non-allocated memory, it would double deallocate
allocated memory.


Best

Kai-Uwe Bux

Ole Nielsby
Guest
 
Posts: n/a
#6: Oct 11 '06

re: auto_ptr



LR <lruss@superlink.netwrote:
Quote:
Ron Natalie wrote:
Quote:
>This isn't @*^&@! JAVA. If you want a local object, use a
>local object.
>>
> void myFun() {
> T x;
> thirdPartyLibFun(&x);
> }
>>
>Plenty exception safe and a bit more efficient.
>
Do you hae the souce code for thirdPartyLibFun(T *t)?
What if it deletes the T that is pointed to?
If it does there is trouble anyway. An auto_ptr shouldn't
point to an object that gets deleted by something else.
Consider what would happen if the deleted memory
was reallocated for some other purpose - the auto_ptr
would have no clue and still attepmt to delete it.



Clark S. Cox III
Guest
 
Posts: n/a
#7: Oct 11 '06

re: auto_ptr


LR wrote:
Quote:
Ron Natalie wrote:
Quote:
>bb wrote:
>>
Quote:
>>Hi,
>>>
>>Is the following safe? (it works fine and to me it looks exception
>>safe!).
>>>
>> void thirdPartyLibFun(T* t) {
>> ...
>> }
>>>
>> void myFun() {
>> std::auto_ptr<Tap2t = std::auto_ptr<T>(new T);
>>
>>
>Try direct initialization, lose the cast.
>>
Quote:
>>>
>> thirdPartyLibFun(ap2t.get());
>> }
>>
>>
>This isn't @*^&@! JAVA. If you want a local object, use a
>local object.
>>
> void myFun() {
> T x;
> thirdPartyLibFun(&x);
> }
>>
>Plenty exception safe and a bit more efficient.
>
Do you hae the souce code for thirdPartyLibFun(T *t)? What if it
deletes the T that is pointed to?
>
LR
Then the code is broken regardless of auto_ptr's involvement.


--
Clark S. Cox III
clarkcox3@gmail.com
Closed Thread