Serving the Quantitative Finance Community

 
User avatar
dougal12
Topic Author
Posts: 0
Joined: November 23rd, 2005, 2:16 pm

How to make a smart pointer which can be passed as a long*

December 14th, 2011, 12:26 pm

I have a function double oldFn( long * ptrLong ). It is somewhat legacy code which I don't want to change (before anyone suggests it). It takes in a long*, does some manipulations and returns a double (let us say). I want to pass in an input which is an array (called lIndex below) with 2 elements both set equal to one (this is what oldFn needs to do what I want it to do).Trivially, I could do:long* lIndex = new long[2];lIndex[0] = 1;lIndex[1] = 1;double tmp = oldFn( lIndex );delete lIndex;lIndex = NULL;// do something interesting with tmpThe problem is that if oldFn throws, then I leak memory. I would have thought that I could use a Boost smart pointer. Unfortunately, I do need lIndex[0] = 1; and lIndex[1] = 1; on entry to the function oldFn. All the smart pointers (at least to my limited understanding - I am a novice) seem to be const ie once created, I can't change them to fill in the two elements to equal 1. Has anyone got any good ideas? I am sure there is a three line solution. Basically, I want to use a Boost or STL construction that does this and is guaranteed to not leak memory (and also so I don't need to change oldFn( long * ptrLong ))?Thanks in anticipation of your help. Doug
 
User avatar
imdx80
Posts: 0
Joined: October 18th, 2002, 10:23 am

How to make a smart pointer which can be passed as a long*

December 14th, 2011, 1:01 pm

....<ignore>
Last edited by imdx80 on December 13th, 2011, 11:00 pm, edited 1 time in total.
 
User avatar
Jim
Posts: 1
Joined: February 1st, 2002, 5:20 pm

How to make a smart pointer which can be passed as a long*

December 14th, 2011, 1:42 pm

First, since you are allocating an array you need to call "delete [] llndex" not just "delete llndex". Next, to make use of smart pointers you can use shared_array in Boost (not just shared_ptr) like so:#include <boost/shared_array.hpp>use namespace boost;// .// .// .shared_array<long> llndex(new long[2]);llndex[0] = 1;llndex[1] = 1;double tmp = oldFn(llndex.get());But to be honest, I would just use an STL vector and pass the address of element 0, like so:vector<long> llndex(2, 1); // allocates a vector of length 2 with each element initialized to 1double tmp = oldFn(&llndex[0]);
 
User avatar
farmer
Posts: 63
Joined: December 16th, 2002, 7:09 am

How to make a smart pointer which can be passed as a long*

December 14th, 2011, 2:07 pm

QuoteOriginally posted by: dougal12The problem is that if oldFn throws, then I leak memory.In Symbian, they solve this with something called the cleanup stack.var * myvar = new var();addtocleanup(myvar);doriskythings(myvar);removefromcleanup(myvar);delete myvar;
Antonin Scalia Library http://antoninscalia.com
 
User avatar
dougal12
Topic Author
Posts: 0
Joined: November 23rd, 2005, 2:16 pm

How to make a smart pointer which can be passed as a long*

December 14th, 2011, 2:11 pm

Thanks a lot, Jim. Your second suggestion: (vector<long> llndex(2, 1); double tmp = oldFn(&llndex[0]) works perfectly (didn't try the first 'coz the second suggestion works great). Thanks a lot for your help.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

How to make a smart pointer which can be passed as a long*

December 14th, 2011, 3:03 pm

Last edited by Cuchulainn on December 13th, 2011, 11:00 pm, edited 1 time in total.