realloc 和 memcpy 如何工作? [英] How do realloc and memcpy work?

查看:20
本文介绍了realloc 和 memcpy 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题.

  1. realloc()memcpy() 将数组中的条目复制到另一个数组中的方式比仅迭代每个元素要快 O(N) ?如果答案是肯定的,那么您认为它的复杂性如何?

  1. Do realloc() and memcpy() copy the entries in an array to another in a way faster than just iterating on each element O(N) ? If the answer is yes then what do you think is its complexity ?

如果分配的大小小于原始大小,realloc() 是将条目复制到其他地方,还是只保留它们,因为它们正在减小数组的大小?p>

If the size allocated is smaller than the original size, does realloc() copy the entries to somewhere else or just leave them as they are decreasing the size of the array ?

推荐答案

1 - 不.他们一次复制一个块.请参阅 http://www.embedded.com/design/configure-systems/4024961/Optimizing-Memcpy-improves-speed 进行了很好的分析.

1 - No. They copy a block at a time. See http://www.embedded.com/design/configurable-systems/4024961/Optimizing-Memcpy-improves-speed for a pretty good analysis.

2 - 这取决于实现.参见 http://www.gnu.org/software/libtool/manual/libc/Changing-Block-Size.html 了解 glibc 的详细信息.在多个分配实现中,有时需要将块变小,从而需要复制它"

2 - This is implementation dependent. See http://www.gnu.org/software/libtool/manual/libc/Changing-Block-Size.html for glibc details. "In several allocation implementations, making a block smaller sometimes necessitates copying it"

这篇关于realloc 和 memcpy 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆