[Insight-developers] itkGetConstMacro versus itkGetConstReferenceMacro

Luis Ibanez luis.ibanez at kitware.com
Mon Nov 8 09:15:45 EST 2004


Hi Samson,

You are right, that will be a situation in which
the reference will become outdated.

For that type of situations where the connection to the
object returned by "Get" is supposed to be persistant we
use Smart Pointers, and any of the ITK containers, such
as the itk::VectorContainer and the itk::MapContainer.


   Regards,


     Luis


--------------------
Samson Timoner wrote:

> Luis Ibanez wrote:
> 
>>
>> I agree in that returning a const reference is always better
>> than returning the copy.
> 
> 
> I've found that this statement is mostly, but not always true.
> 
> It falls apart for variable length arrays. Imagine having a reference 
> into a variable length array which is full. Imagine adding an element, 
> which causes the array to be re-allocated, with the all the old elements 
> copied into the new array. But, your reference still points to the old 
> array which no longer exists...
> 
> It's a subtle bug which I was forced to track down in a different library.
> 
> When the world is parallelized, I believe similar bugs exists when 
> passing references between processors and/or machines.
> 
> I didn't look at Bill's changes. It is highly likely they are fine, but 
> passing a reference is, unfortunately, not always the right thing to do.
> 
> -- Samson
> 
> P.S. My aplologies Luis, I'm currently only available to use one of my 
> e-mail addresses and I don't think it is the one on the itk-developers 
> list, so I suspect this note will be bounced from the list.
> 
> 
>>
>> Actually it seems by having
>>
>>       itkGetConstReferenceMacro()   and
>>       itkGetConstObjectMacro()
>>
>> we shouldn't need
>>
>>       itkGetConstMacro().
>>
>>
>>
>>   Luis
>>
>>
>> -----------------------------------------
>> Lorensen, William E (Research) wrote:
>>
>>> Jim and I found a nasty bug yesterday that was related to the use of
>>> GetConstMacro. It's a long story, but the Get should have returned a
>>> reference rather than a copy.
>>>
>>> Last night, I replaced all GetConstMacro usage with 
>>> GetConstReferenceMacro.
>>> Everything compiled and all tests passed on Borland and VS7.
>>>
>>> Can anyonoe think of a reason why this would be a bad move. Certainly,
>>> performace should be slightly better with the reference macro since a 
>>> new
>>> version of the ivar need not be created.
>>>
>>> If there are no objections, I'll check this in later today. I'm building
>>> InsightApplications now.
>>>
>>> Bill
>>> _______________________________________________
>>> Insight-developers mailing list
>>> Insight-developers at itk.org
>>> http://www.itk.org/mailman/listinfo/insight-developers
>>>
>>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Insight-developers mailing list
>> Insight-developers at itk.org
>> http://www.itk.org/mailman/listinfo/insight-developers
> 
> 
> 






More information about the Insight-developers mailing list