bod*_*ydo 78 c++ realloc new-operator delete-operator
我怎么能用realloc
C++?它似乎在语言中缺失 - 有new
,delete
但不是resize
!
我需要它,因为当我的程序读取更多数据时,我需要重新分配缓冲区来保存它.我不认为delete
旧指针和new
新的更大的指针是正确的选择.
f0b*_*b0s 47
使用:: std :: vector!
Type* t = (Type*)malloc(sizeof(Type)*n)
memset(t, 0, sizeof(Type)*m)
Run Code Online (Sandbox Code Playgroud)
变
::std::vector<Type> t(n, 0);
Run Code Online (Sandbox Code Playgroud)
然后
t = (Type*)realloc(t, sizeof(Type) * n2);
Run Code Online (Sandbox Code Playgroud)
变
t.resize(n2);
Run Code Online (Sandbox Code Playgroud)
如果你想将指针传递给函数,而不是
Foo(t)
Run Code Online (Sandbox Code Playgroud)
使用
Foo(&t[0])
Run Code Online (Sandbox Code Playgroud)
这是绝对正确的C++代码,因为vector是一个智能C-array.
Tho*_*mas 45
正确的选择可能是使用一个能为你工作的容器,比如std::vector
.
new
并且delete
无法调整大小,因为它们分配的内存足以容纳给定类型的对象.给定类型的大小永远不会改变.有new[]
,delete[]
但几乎没有理由使用它们.
什么realloc
做用C很可能只是一个malloc
,memcpy
并且free
,无论如何,虽然内存管理器被允许这样做的很漂亮,如果有足够的可用连续可用内存.
Ste*_*sop 33
在C++中调整大小是不方便的,因为可能需要调用构造函数和析构函数.
我不认为有一个根本原因在C++中,你可以没有一个resize[]
运营商去与new[]
和delete[]
,即没有类似的东西,以这样的:
newbuf = new Type[newsize];
std::copy_n(oldbuf, std::min(oldsize, newsize), newbuf);
delete[] oldbuf;
return newbuf;
Run Code Online (Sandbox Code Playgroud)
Obviously oldsize
would be retrieved from a secret location, same is it is in delete[]
, and Type
would come from the type of the operand. resize[]
would fail where the Type is not copyable - which is correct, since such objects simply cannot be relocated. Finally, the above code default-constructs the objects before assigning them, which you would not want as the actual behaviour.
There's a possible optimisation where newsize <= oldsize
, to call destructors for the objects "past the end" of the newly-ensmallened array and do nothing else. The standard would have to define whether this optimisation is required (as when you resize()
a vector), permitted but unspecified, permitted but implementation-dependent, or forbidden.
The question you should then ask yourself is, "is it actually useful to provide this, given that vector
also does it, and is designed specifically to provide a resize-able container (of contiguous memory--that requirement omitted in C++98 but fixed in C++03) that's a better fit than arrays with the C++ ways of doing things?"
I think the answer is widely thought to be "no". If you want to do resizeable buffers the C way, use malloc / free / realloc
, which are available in C++. If you want to do resizeable buffers the C++ way, use a vector (or deque
, if you don't actually need contiguous storage). Don't try to mix the two by using new[]
for raw buffers, unless you're implementing a vector-like container.
归档时间: |
|
查看次数: |
69432 次 |
最近记录: |