C++ Built-In Array??????
???????????? ???????[ 2014/11/18 16:06:22 ] ??????????????? C++ .NET
????A ???????????????destructor????
????????destructor??A a[10] ?????????????μ?仯?????????????д?????object“???”????????????????“???”?????????е????
??????????????dummy ????????destructor??array ?????
struct StackObject2
{
int _a;
int _b;
StackObject2(): _a(0)?? _b(1)
{
}
~StackObject2()
{
_a = _b = 0;
}
};
??????????????????
void TestArraySemantics()
{
clock_t begin = clock();
for (int i = 0; i < 100000; ++i)
{
StackObject sa[3]; //line1??test without d’tr
sa[0]._a= 1;
sa[1]._b = 2;
sa[2]._b = 3;
}
clock_t end = clock();
auto spent = double(end - begin) / CLOCKS_PER_SEC;
printf("spent for 'array' is %f
"?? spent);
begin = clock();
for (int i = 0; i < 100000; ++i)
{
StackObject2 sa[3]; //line2??test with d’tr
sa[0]._a= 1;
sa[1]._b = 2;
sa[2]._b = 3;
}
end = clock();
spent = double(end - begin) / CLOCKS_PER_SEC;
printf("spent for 'array with dtro' is %f
"?? spent);
begin = clock();
for (int i = 0; i < 100090; ++i)
{
StackObject sa1?? sa2?? sa3; //line3??test without array
sa1._a= 1;
sa2._b = 2;
sa3._b = 3;
}
end = clock();
spent = double(end - begin) / CLOCKS_PER_SEC;
printf("spent for 'none-array' is %f
"?? spent);
}
?????????line2???????????destructor??StackObject2???array. ??VC++?????????????????????????????????????“eh vector constructor iterator”???????????????????????vector constructor iterator???????????α?????????
void Vector_constructor_iterator_with_dtor(
int array_size??
int array_element_size??
void (*Ctr)(void *addr)??
void (*Dtr)(void *addr)??
char *arrayStartAddress)
{
int lastCreated = -1;
try
{
for(int i = 0; i < array_size; ++i)
{
void *objAddr = arrayStartAddress + i * array_element_size;
Ctr(objAddr);
lastCreated = i;
}
}
catch(...)
{
// destroy partially created array in case or fault
for(int i = 0; i <= lastCreated; ++i)
{
void *objAddr = arrayStartAddress + i * array_element_size;
Dtr (objAddr);
}
}
}
???????α???????????Vector_constructor_iterator_with_dtor ???Vector_constructor_iterator??????????????????????????????????????“???????”?????????
???????С?TestArraySemantics????????????destructor?????array???????????????300%??
???????????????????destructor???????????γ???????
????????
????C++ built-in array??class object??????????????????????????C++??class object???????????????????????????????????C++????????
????????????????????????C++???????????????????????????????C++ built-in array??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11