bstr和delete [] [英] bstr and delete[]
问题描述
你好,
delete []如何知道从给定的
指针释放多少内存? AFAIK这个信息由new []放在那里。 new []在
数组开始之前放置已分配内存的
大小。但是通过查看内存我无法找到这些信息。
(通过VS2005 - C ++)
我的第二个问题是,如果有的话机制知道为阵列分配了多少内存
,为什么我们不把它用于像bstr这样的东西?
我能想到的只有你可以用新的[]分配一大块
内存,并将其用于多个bstr'等等。 (但是
我甚至不确定如何使用bstr,因为你必须使用
:: SysAllocString)
提前谢谢,
Furkan
2月20日下午2:38 ,yufufi< yufufi_at_comcast.netwrote:
delete []如何知道从给定的
指针释放多少内存? AFAIK这个信息由new []放在那里。 new []在
数组开始之前放置已分配内存的
大小。但是我无法通过查看内存来找到这些信息。
(通过VS2005 - C ++)
http://www.parashift.com/c++-faq-lit...html#faq -16.14
我的第二个问题是,如果有机制知道为阵列分配多少内存
,为什么我们不把它用于像bstr这样的东西?
只有我能想到的是你可以用新的分配一大块
内存]并使用它超过一个bstr'等。 (但是
我甚至不确定如何使用bstr,因为你必须使用
:: SysAllocString)
这部分是偏离主题的。尝试一个Microsoft小组(参见
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9 )
干杯! --M
mlimber写道:
2月20日下午2:38, yufufi< yufufi_at_comcast.netwrote:
> delete []如何知道从给定的
指针释放多少内存? AFAIK这个信息由new []放在那里。 new []在
数组开始之前放置已分配内存的大小。但是通过查看内存我无法找到这些信息。
(通过VS2005 - C ++)
http://www.parashift.com/c++-faq-lit...html#faq -16.14
>我的第二个问题是,如果有机制知道为阵列分配了多少内存,为什么我们不是把它用于像bstr这样的东西吗?
我唯一能想到的就是你可以用新的[]来分配一大块内存,并将它用于超过一个bstr'等等。 (但是我甚至不确定你怎么能用bstr来做,因为你必须使用
:: SysAllocString)
这个部分是偏离主题的。尝试一个Microsoft小组(参见
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9 )
干杯! --M
您指向的页面回答了有多少个物品要敲诈?b $ b destruct?的问题。这与我要问的有点不同。
在其中一个delete []实现的示例代码中:
//原始代码:Fred * p = new Fred [n];
char * tmp =(char *)operator new [](WORDSIZE + n * sizeof(Fred));
Fred * p =(Fred *)(tmp + WORDSIZE);
*(size_t *)tmp = n;
size_t i;
尝试{
for(i = 0; i< n; ++ i)
new(p + i)Fred(); //安置新的
}
catch(...){
while(i--!= 0)
(p + i) - > ~Fred(); //显式调用析构函数
operator delete []((char *)p - WORDSIZE);
throw;
}
''n''用于决定所需的析构函数调用次数。但是在最后一行之前是
:
运算符delete []((char *)p - WORDSIZE);
>
静态编译器带有一个指向数组的指针(在它的第一个字符号为
,第一个字符号为n)
1)Isn这是一个无限循环(递归)?
2)如果第二个是某种不同的删除[],它会表现如下:
operator delete [] (* p)
{
arraySize =(((WORDSIZE *)p)[0])* sizeOf(Fred)
InternalDelete( p,arraySize);
}
谢谢,
yufufi写道:
mlimber写道:
> 2月20日下午2:38,yufufi< yufufi_at_comcast.netwrote:
>> delete []如何知道从给定的
指针释放多少内存? AFAIK这个信息由new []放在那里。 new []在
数组开始之前放置已分配内存的大小。但是通过查看内存我无法找到这些信息。
(通过VS2005 - C ++)
http://www.parashift.com/c++-faq-lit...html#faq -16.14
>>我的第二个问题是,如果有机制知道为阵列分配了多少内存,为什么我们不把它用于像bstr这样的东西?
我唯一能想到的就是你可以用新的[]分配一大块内存并将其用于一个以上bstr'等等。 (但是我甚至不确定你怎么能用bstr来做,因为你必须使用
:: SysAllocString)
这部分是关闭的 - 话题在这里。尝试一个Microsoft小组(参见
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9 )
干杯! --M
您指向的页面回答了有多少个物品要求/ b $ b毁坏?的问题。这与我要问的有点不同。
在其中一个delete []实现的示例代码中:
你很困惑。这不是delete []实现 - 这是一个
new []实现。对运算符delete []的调用是在一个catch块
中,这是为了防止内存泄漏,如果构造函数之一
" call" Fred(通过贴牌新)失败。
>
//原始代码:Fred * p = new Fred [n];
char * tmp =(char *)operator new [](WORDSIZE + n * sizeof(Fred));
Fred * p =(Fred *)(tmp + WORDSIZE);
*(size_t *)tmp = n;
size_t i;
试试{
for (i = 0; i< n; ++ i)
new(p + i)Fred(); //安置新的
}
catch(...){
while(i--!= 0)
(p + i) - > ~Fred(); //显式调用析构函数
operator delete []((char *)p - WORDSIZE);
throw;
}
''n''用于决定所需的析构函数调用次数。但是在最后一行之前是
:
运算符delete []((char *)p - WORDSIZE);
>
静态编译器带有一个指向数组的指针(在它的第一个字符号为
,第一个字符号为n)
1)Isn这是一个无限循环(递归)?
否。正如我已经说过的,这是一个新的[]而不是
delete []的实现。更多的问题,新[]和operator new []之间有区别
,同样对于delete []和operator
delete [] 。第一个版本不仅通过operator new [](或operator delete [])分配(或解除分配)内存
,它们还构造(或
destruct)数组对象。第二个版本基本上只是关于低级别分配和释放的
。
2)如果第二个版本是某种不同的删除[],它的行为如下:
operator delete [](* p)
{
arraySize =(((WORDSIZE *)p )[0])* sizeOf(Fred)
InternalDelete(p,arraySize);
}
类似的东西,对于一个适当定义的InternalDelete
函数。它需要释放运算符new []
上面分配的内存,即:WORDSIZE + n * sizeof(Fred)。
-Mark
Hello,
How does delete[] know how much memory to deallocate from the given
pointer? AFAIK this informations is put there by new[]. new[] puts the
size of the allocated memory before the just before the beginning of the
array. But I couldn''t find this information by looking at the memory.
(Via VS2005 - C++)
My second questions is, if there is a mechanism to know how much memory
is allocated for the array, why don''t we use it for things like bstr?
Only thing I can think of is you may go and allocate a big chunk of
memory with new[] and use it for more then one bstr''s and such. (However
i''m not even sure how you can do it with bstr since you have to use
::SysAllocString)
Thank you in advance,
Furkan
On Feb 20, 2:38 pm, yufufi <yufufi_at_comcast.netwrote:How does delete[] know how much memory to deallocate from the given
pointer? AFAIK this informations is put there by new[]. new[] puts the
size of the allocated memory before the just before the beginning of the
array. But I couldn''t find this information by looking at the memory.
(Via VS2005 - C++)http://www.parashift.com/c++-faq-lit...html#faq-16.14
My second questions is, if there is a mechanism to know how much memory
is allocated for the array, why don''t we use it for things like bstr?
Only thing I can think of is you may go and allocate a big chunk of
memory with new[] and use it for more then one bstr''s and such. (However
i''m not even sure how you can do it with bstr since you have to use
::SysAllocString)This part is off-topic here. Try a Microsoft group (cf. the list at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9)
Cheers! --M
mlimber wrote:On Feb 20, 2:38 pm, yufufi <yufufi_at_comcast.netwrote:>How does delete[] know how much memory to deallocate from the given
pointer? AFAIK this informations is put there by new[]. new[] puts the
size of the allocated memory before the just before the beginning of the
array. But I couldn''t find this information by looking at the memory.
(Via VS2005 - C++)
http://www.parashift.com/c++-faq-lit...html#faq-16.14
>My second questions is, if there is a mechanism to know how much memory
is allocated for the array, why don''t we use it for things like bstr?
Only thing I can think of is you may go and allocate a big chunk of
memory with new[] and use it for more then one bstr''s and such. (However
i''m not even sure how you can do it with bstr since you have to use
::SysAllocString)
This part is off-topic here. Try a Microsoft group (cf. the list at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9)
Cheers! --M
The page you point to answers the question of "How many objects to
destruct?" which is a little bit different then what I''m asking.
In the sample code for one of the delete[] implementations:
// Original code: Fred* p = new Fred[n];
char* tmp = (char*) operator new[] (WORDSIZE + n * sizeof(Fred));
Fred* p = (Fred*) (tmp + WORDSIZE);
*(size_t*)tmp = n;
size_t i;
try {
for (i = 0; i < n; ++i)
new(p + i) Fred(); // Placement new
}
catch (...) {
while (i-- != 0)
(p + i)->~Fred(); // Explicit call to the destructor
operator delete[] ((char*)p - WORDSIZE);
throw;
}
''n'' used to decide number of destructor calls needed. But in the line
one before the last:
operator delete[] ((char*)p - WORDSIZE);
Still compiler is presented with a pointer to an array(which in it''s
first WORDSIZE has n)
1) Isn''t this an infinite loop(recursive)?
2) If the second one is somehow a different delete[], will it act like:
operator delete[] (*p)
{
arraySize = (((WORDSIZE *)p)[0])*sizeOf(Fred)
InternalDelete(p,arraySize);
}
Thank you,
yufufi wrote:mlimber wrote:>On Feb 20, 2:38 pm, yufufi <yufufi_at_comcast.netwrote:>>How does delete[] know how much memory to deallocate from the given
pointer? AFAIK this informations is put there by new[]. new[] puts the
size of the allocated memory before the just before the beginning of the
array. But I couldn''t find this information by looking at the memory.
(Via VS2005 - C++)
http://www.parashift.com/c++-faq-lit...html#faq-16.14
>>My second questions is, if there is a mechanism to know how much memory
is allocated for the array, why don''t we use it for things like bstr?
Only thing I can think of is you may go and allocate a big chunk of
memory with new[] and use it for more then one bstr''s and such. (However
i''m not even sure how you can do it with bstr since you have to use
::SysAllocString)
This part is off-topic here. Try a Microsoft group (cf. the list at
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9)
Cheers! --M
The page you point to answers the question of "How many objects to
destruct?" which is a little bit different then what I''m asking.
In the sample code for one of the delete[] implementations:You''re confused. This is not a delete[] implementation-- this is a
new[] implementation. The call to operator delete[] is in a catch block
which is there to prevent a memory leak in case one of the constructor
"calls" to Fred (via placement new) fails.
>
// Original code: Fred* p = new Fred[n];
char* tmp = (char*) operator new[] (WORDSIZE + n * sizeof(Fred));
Fred* p = (Fred*) (tmp + WORDSIZE);
*(size_t*)tmp = n;
size_t i;
try {
for (i = 0; i < n; ++i)
new(p + i) Fred(); // Placement new
}
catch (...) {
while (i-- != 0)
(p + i)->~Fred(); // Explicit call to the destructor
operator delete[] ((char*)p - WORDSIZE);
throw;
}
''n'' used to decide number of destructor calls needed. But in the line
one before the last:
operator delete[] ((char*)p - WORDSIZE);
Still compiler is presented with a pointer to an array(which in it''s
first WORDSIZE has n)
1) Isn''t this an infinite loop(recursive)?No. As I already said, this is an implementation of new[] not of
delete[]. More to the point of your question, there''s a distinction
between new[] and operator new[], and likewise for delete[] and operator
delete[]. The first versions not only allocate (or deallocate) memory
via operator new[] (or operator delete[]), they also construct (or
destruct) the array of objects. The second versions are basically
concerned with just low-level allocation and deallocation.
2) If the second one is somehow a different delete[], will it act like:
operator delete[] (*p)
{
arraySize = (((WORDSIZE *)p)[0])*sizeOf(Fred)
InternalDelete(p,arraySize);
}Something like that, for an appropriately defined InternalDelete
function. It needs to free the memory allocated above by operator new[]
which was: WORDSIZE + n * sizeof( Fred).
-Mark
这篇关于bstr和delete []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!