[MOAB-dev] r4250 - MOAB/trunk/tools/dagmc

Tim Tautges tautges at mcs.anl.gov
Thu Nov 4 05:05:27 CDT 2010



On 11/03/2010 11:15 PM, Paul Wilson wrote:
> Hello,
>
> To be clear - while the former may not be valid, the replacement is
> actually semantically different.
>
> The first declares a pointer to a C-style array of nvert objects of size
> CartVect. The second uses an STL vector for the same.
>

Correct, I should have stated: all uses in this case were coords[...], which is a valid syntax for stl vectors.

> Theoretically, a standard-compliant replacement could be...
>
> CartVect *coords;
> coords = new CartVect(nverts);
>

The disadvantage of this usage is you have to remember to delete, which can be a nuisance if you have multiple exit points.

> I ask because the valid set of operations on variable 'coords' changes
> with the change you made and it seems possible that other places would
> need to be modified accordingly. Or is that new set of operations a
> strict superset of the original operations?
>

In almost all cases it'll be a superset.  The one common exception I can think of is if you were passing the array 
pointer, coords, to another function; the stl vector equivalent would be &coords[0] for that.

> And while I am asking tutorial questions... is the original a problem
> because the notation is only correct (in a standards compliant way) for
> only constant sized arrays (compile time)?
>

Yep.  Ran across this one all the time when I taught that parallel computing course.  I wish gnu wouldn't allow that 
extension.

- tim

> Paul
>
>
> On 11/03/2010 08:19 PM, tautges at mcs.anl.gov wrote:
>> Author: tautges
>> Date: 2010-11-03 20:19:00 -0500 (Wed, 03 Nov 2010)
>> New Revision: 4250
>>
>> Modified:
>> MOAB/trunk/tools/dagmc/quads_to_tris.cpp
>> Log:
>> Replacing non-standard line:
>>
>> CartVect coords[nverts]
>>
>> with
>> std::vector<CartVect> coords(nverts)
>>
>> This mistake is fairly common, and gcc allows it when it really
>> shouldn't.
>>
>>
>>
>> Modified: MOAB/trunk/tools/dagmc/quads_to_tris.cpp
>> ===================================================================
>> --- MOAB/trunk/tools/dagmc/quads_to_tris.cpp 2010-11-04 01:02:37 UTC
>> (rev 4249)
>> +++ MOAB/trunk/tools/dagmc/quads_to_tris.cpp 2010-11-04 01:19:00 UTC
>> (rev 4250)
>> @@ -32,7 +32,7 @@
>> assert( MB_SUCCESS == result);
>>
>> // find length of diagonals
>> - CartVect coords[n_verts];
>> + std::vector<CartVect> coords(n_verts);
>> result = MBI->get_coords( quad_conn, n_verts, coords[0].array() );
>> if(MB_SUCCESS != result) return result;
>> CartVect diagA = coords[0] - coords[2];
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>

-- 
================================================================
"You will keep in perfect peace him whose mind is
   steadfast, because he trusts in you."               Isaiah 26:3

              Tim Tautges            Argonne National Laboratory
          (tautges at mcs.anl.gov)      (telecommuting from UW-Madison)
          phone: (608) 263-8485      1500 Engineering Dr.
            fax: (608) 263-4499      Madison, WI 53706



More information about the moab-dev mailing list