Crash when declaring 2D dynamic arrays


#1

Hi,

In this example I’m defining an array test as { {0,1}, {0,1,2} }

ON_SimpleArray<ON_SimpleArray<int>> test;
test.Reserve(2);
test.SetCount(2);

test[0].Reserve(2);
test[0].SetCount(2);
test[0][0] = 0;
test[0][1] = 1;

test[1].Reserve(3);
test[1].SetCount(3);
test[1][0] = 0;
test[1][1] = 1;
test[1][2] = 2;

When executing this, the arrays test[0] and test[1] seem to have always Count at 0. The lines of Reserve give the crash. Apparently the sizes cannot be increased from 0.

What’s the proper way to define this? In case this construct turns out to be inefficient, is there a better way to create arrays with arrays as components?

Thanks a lot,
Pablo


(Menno Deij - van Rijswijk) #2

You should use SetCapacity(int size) instead of Reserve.


(Chuck Welsh) #3

ON_SimpleArray is for structs and very simple classes only.
You need to use

ON_ClassArray<ON_SimpleArray<int> > test;

#4

@menno I tried SetCapacity but didn’t work neither…

@chuck yeah it worked!

Many thanks,
Pablo