c++ - unique_ptr<TStringList []> dsts(new TStringList[5]) fail -


myenvironment:

c++ builder xe4 

i trying use array of tstringlist using unique_ptr<>.

following no problem:

unique_ptr<int []> vals(new int [10]); 

on other hand, following shows error:

unique_ptr<tstringlist []> sls(new tstringlist [10]); 

the error 'access violation @ 0x000000000: read of address 0x0000000'.

for tstringlist, can't use array of unique_ptr<>?

it isn't unique_ptr issue: attempt fails because trying create array of actual tstringlist object instances instead of array of pointers tstringlist instances (for further details can take @ how create array of buttons on borland c++ builder , work it? , quality central report #78902).

e.g. you'll access violation if try:

tstringlist *sls(new tstringlist[10]); 

(pointer dynamic array of size 10 , type tstringlist).

you have manage pointer dynamic array of type tstringlist *. using std::unique_ptr:

std::unique_ptr< std::unique_ptr<tstringlist> [] > sls(     new std::unique_ptr<tstringlist>[10]);  sls[0].reset(new tstringlist); sls[1].reset(new tstringlist);  sls[0]->add("test 00"); sls[0]->add("test 01"); sls[1]->add("test 10"); sls[1]->add("test 11");  showmessage(sls[0]->text); showmessage(sls[1]->text); 

anyway, if size known @ compile time, better choice:

boost::array<std::unique_ptr<tstringlist>, 10> sls; 

(also take @ is there use unique_ptr array?)


Comments