**THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED**

*On 12/12/2008 at 08:58, ***xxxxxxxx** wrote:

This is the original code from Thomas Luft's Ivy Generator (which worked in VC++ 6 for testing and Xcode 2 when I ported it to MacOS for him) :

> `for (std::vector<IvyRoot>::iterator root = roots.begin(); root != roots.end(); ++root) \> { \> for (int g = 0; g < 5; ++g) \> { \> for (std::vector<IvyNode>::iterator node = root->nodes.begin(); node != root->nodes.end(); ++node) \> { \> Vector3d e; \> \> for (int i = -5; i <= 5; ++i) \> { \> Vector3d tmpAdhesion; \> \> if ((node + i) < root->nodes.begin()) tmpAdhesion = root->nodes.front().adhesionVector; \> if ((node + i) >= root->nodes.end()) tmpAdhesion = root->nodes.back().adhesionVector; \> if (((node + i) >= root->nodes.begin()) && ((node + i) < root->nodes.end())) tmpAdhesion = (node + i)->adhesionVector; \> \> e += tmpAdhesion \* gaussian[i+5]; \> } \> \> node->smoothAdhesionVector = e / 56.0f; \> } \> \> for (std::vector<IvyNode>::iterator node = root->nodes.begin(); node != root->nodes.end(); ++node) \> { \> node->adhesionVector = node->smoothAdhesionVector; \> } \> } \> }`

Note the 'node + i' references. These are what immediately crashed with VS2005 build. Also seems that the out-of-bound indices aren't stable for iterators. So, I looked around and decided to go with the [] operator to index into the nodes vector:

This was the modified code which still crashed:

> `int g, i; \> LONG n; \> Vector e; \> Vector tmpAdhesion; \> std::vector<IvyRoot>::iterator root; \> std::vector<IvyNode>::iterator node; \> for (root = roots.begin(); root != roots.end(); ++root) \> { \> for (g = 0; g != 5; ++g) \> { \> for (node = root->nodes.begin(); node != root->nodes.end(); ++node) \> { \> n = 0L; \> e = Vector(0.0f); \> for (i = -5; i != 6; ++i) \> { \> // Behavior for these two was discovered to be indeterminate - but no crashing! \> if ((root->nodes[n+i]) < root->nodes.begin()) tmpAdhesion = root->nodes.front().adhesionVector; \> else if (root->nodes[n+i] >= root->nodes.end()) tmpAdhesion = root->nodes.back().adhesionVector; \> // This line led to the crashes!! \> else tmpAdhesion = (root->nodes[n+i]).adhesionVector; \> e += tmpAdhesion \* gaussian[i+5]; \> } \> ++n; \> node->smoothAdhesionVector = e \* 0.017857142857142857142857142857143f; // divided by 56 \> } \> for (node = root->nodes.begin(); node != root->nodes.end(); ++node) \> { \> node->adhesionVector = node->smoothAdhesionVector; \> } \> } \> }`

So, I changed it again to this and it works perfectly (?) :

> `int g, i; \> LONG n; \> LONG ln; \> Vector e; \> Vector tmpAdhesion; \> std::vector<IvyRoot>::iterator root; \> std::vector<IvyNode>::iterator node; \> for (root = roots.begin(); root != roots.end(); ++root) \> { \> ln = (LONG)root->nodes.size(); \> for (g = 0; g != 5; ++g) \> { \> for (node = root->nodes.begin(); node != root->nodes.end(); ++node) \> { \> n = 0L; \> e = Vector(0.0f); \> for (i = -5; i != 6; ++i) \> { \> if ((n+i) < 0L) tmpAdhesion = root->nodes.front().adhesionVector; \> else if ((n+i) >= ln) tmpAdhesion = root->nodes.back().adhesionVector; \> // nodes[n+i]=crash : nodes.at(n+i)=no crash \> else tmpAdhesion = (root->nodes.at(n+i)).adhesionVector; \> e += tmpAdhesion \* gaussian[i+5]; \> } \> ++n; \> node->smoothAdhesionVector = e \* 0.017857142857142857142857142857143f; // divided by 56 \> } \> for (node = root->nodes.begin(); node != root->nodes.end(); ++node) \> { \> node->adhesionVector = node->smoothAdhesionVector; \> } \> } \> }`