STL向量是realloc的更好版本吗?

发布于 2024-07-17 06:29:54 字数 346 浏览 2 评论 0原文

我相信,在 C++ 中,处理重新分配的更好方法是使用 STL 向量,因为它保证了连续的存储位置。

我有几个问题来理解差异:

  1. 是否有任何场景我需要更喜欢 realloc 通过向量?
  2. 还有什么(除了向量)相当于 realloc在 C++ 中?

In C++, I believe, a better way of dealing with reallocation is to use a STL vectors, as it guarantees the contiguous storage locations.

I have couple question to understand the difference:

  1. Is there any scenario in which I need to prefer realloc over vector ?
  2. Is there anything else ( apart from vector ) which is equivalent to realloc in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇俏 2024-07-24 06:29:55

std::vector 的主要好处之一是,当它在内部从自然增长中重新分配自身时,它会选择比当前大小大 2 倍的大小(通常 - 但始终是恒定的乘数)。 这意味着 Push_back 的成本摊销为 O(1)。

Realloc 可以让您更好地控制内存分配方式,但能力越大,责任越大。 如果您所做的一切相当于push_back,并且每次添加元素时都重新分配,那么每次添加到数组时可能都是O(N)操作。

One of the main benefits of std::vector is that when it reallocates itself internally from naturally growing, it chooses a size that is 2x larger than the current size (usually - but always a constant multiplier). This means that push_back's have amortized O(1) cost.

Realloc will give you finer control over how you allocate memory, but with great power comes great responsibility. If you all you are doing is the equivalent of push_back, and you realloc each time you add an element, that is potentially an O(N) operation on each addition to the array.

暮色兮凉城 2024-07-24 06:29:55

我猜这只是一个向量。

我还没有看到有人建议在 C++ 中使用 realloc。

I guess it's only a vector.

I haven't seen anyone who would suggest using realloc in C++.

大姐,你呐 2024-07-24 06:29:54

它只是vector,保证有连续的内存。 不是其他人。

realloc 是一个 C 内存管理函数。 不鼓励在 C++ 代码中使用它。 Stroustrup 告诉您原因:为什么 C++ 没有与 realloc() 等效的函数?

但是,realloc() 只能保证对由 malloc() (和类似函数)分配的数组起作用,该数组包含没有用户定义的复制构造函数的对象。 另外,请记住,与天真的期望相反,realloc() 有时会复制其参数数组。

It is only vector, which is guaranteed to have contiguous memory. Not the others.

realloc is a C memory management function. It's use is not encouraged in C++ code. Here's Stroustrup telling you why: Why doesn't C++ have an equivalent to realloc()?

However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occasionally does copy its argument array.

口干舌燥 2024-07-24 06:29:54

C 函数集(malloc、calloc、realloc、free)是原始内存操作。 他们将在内存中创建/修改/释放给定的缓冲区,但内存将没有类型,并且不会调用构造函数。

C++ 没有 realloc 的等价物,只有通过使用 new/new[] 和 delete/delete[] 来实现 malloc/free 的类型安全等价物/em>. C++ 版本将从系统获取内存并通过调用适当的构造函数来初始化它。 使用delete将调用对象的析构函数,然后释放内存。 C 和 C++ 版本不兼容,如果您使用 malloc 获取内存(即使您在收到的内存上调用就地构造函数),则无法使用 delete/delete[] 释放它> 因为它是未定义的行为。

在 C++ 中使用 realloc 可能不安全,因为它会将对象从一个内存区域按位复制到下一个内存区域。 有时,您的对象无法正确处理内存移动(假设您的对象既有属性又有对其的引用,在按位移动它之后,引用将指向旧位置而不是真实的属性)。 在向量内部,每当内存需要增长时,都会使用new[]获取新的内存区域,然后将所有对象复制到(或在其中构造复制)新位置在删除旧元素之前使用适当的 C++ 操作。

每当向量的大小(保留大小,未使用的大小)增长时,它将创建一个完整的新内存区域并移动所有对象。 另一方面,如果指针后面没有足够的连续空间来增长内存块,realloc 只会将内存块移动到另一个位置。 向量不会减小大小。 绝不。 当您清除元素时,保留的内存仍然保留。

最后,即使对于 POD 类型(可以安全地使用类 C 结构移动),vector 中的抽象级别也比 realloc 更高。 与向量等效的是一个结构体,它保存指向内存缓冲区的指针,已使用的元素计数和保留(缓冲区大小)以及处理根据需要获取更多内存和更新的函数集每个操作的索引。

The set of C functions (malloc, calloc, realloc, free) are raw memory operations. They will create/modify/release a given buffer in memory, but the memory will have no type and no constructors will be called.

C++ does not have an equivalent of realloc, but only typesafe equivalents to malloc/free through the use of new/new[] and delete/delete[]. The C++ versions will both acquire the memory from the system and initialize it by calling the appropriate constructors. Using delete will call the destructors of the objects and then release the memory. C and C++ versions are not compatible, if you acquire memory with malloc (even if you call the inplace constructor on the received memory) you cannot release it with delete/delete[] as it is undefined behavior.

Using realloc in C++ might be unsafe, as it will bitwise copy the objects from one memory area to the next. Sometimes your objects will not deal properly with memory moves (say that your object has both an attribute and a reference to it, after bitwise moving it the reference will be pointing to the old position rather than the real attribute). Inside vector, whenever the memory needs to grow, a new memory area is acquired with new[] and then all objects are copied to (or copy constructed in) the new positions using the appropriate C++ operations before deleting the old elements.

Whenever vector grows in size (reserved size, not used size) it will create a complete new memory area and move all the objects. On the other hand, realloc will only move the memory block to another position if there is not enough contiguous space after the pointer to just grow it. Vectors do not decrease size. Never. When you clear the elements, the reserved memory is still held.

Finally, there is a higher level of abstraction in vector than in realloc even for POD types (that are safe to be moved with C-like constructs). The equivalent to vector would be a structure that holds the pointer to the memory buffer, a used elements count and a reserved (buffer size) and the set of functions that deal with acquiring more memory as needed and updating the indexes with each operation.

花想c 2024-07-24 06:29:54

连续内存也由 realloc 保证,因此这不是不使用它的理由。

然而,我更喜欢在 C++ 中使用向量,因为它处于更高的抽象级别,因此它使代码更容易编写。

我认为在数组类型场景中使用 realloc(通过向量)的唯一可能原因是原始速度。 它可能会更快。 我强调“可能”这个词 - 衡量,不要猜测!

但是,您必须处理自己的重新分配,这是更多的工作。 如果我能更快地交付代码并更快地获得报酬,我宁愿代码运行得慢一点(当然,假设它仍然运行得足够快)。

Contiguous memory is also guaranteed by realloc so that's not a reason not to use it.

However, I would prefer to use a vector in C++ since it's at a higher level of abstraction, hence it makes the code easier to write.

The only possible reason I can think for using realloc (over vector) for an array-type scenario, is raw speed. It may be faster. And I stress the word "may" - measure, don't guess!

However, you have to handle your own reallocations, which is more work. I'd rather have code that runs a little slower (assuming it still runs fast enough, of course), if I can deliver it and get paid quicker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文