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