在 C 中对 10 个整数的数组进行冒泡排序的 RAM 消耗的粗略估计是否可能?
是否有可能在纸上精确估计在一个简单的数据集(10 个整数数组)上用 C 编写的简单算法(冒泡排序)将消耗多少 RAM?或者编译器实现问题和“字节填充”会让这变得不可能吗?
(给定一个平台,例如 32 位 x86 机器)。
Is it possible to precisely estimate on paper how much RAM will be consumed for a simple algorithm (bubble sort) in C on a trivial dataset (10 integer array)? Or will compiler implementation concerns and 'byte padding' make this impossible?
(Given a platform such as a 32bit x86 machine).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
冒泡排序可以就地工作,因此除了您排序的数组之外,它不需要任何内存。
一个 10 整数数组需要 40 个字节,加上一些小的平台相关分配开销。
如果您想要真正精确的估计,则需要考虑可执行文件的大小、用于进程管理的内存等等。但在通常拥有大量内存的 x86 上,这些事情真的没什么好担心的。
如果数组较大,则每个整数需要 4 个字节,并且保持不变的开销可以忽略不计。整数之间没有填充,因此对于大型数组,您应该关心的是每个整数的 4 个字节。
Bubble sort can work in-place, so it needs no memory except the array you sort.
A 10 integer array takes 40 bytes, plus some small platform dependent allocation overhead.
If you want a really precise estimate, you need to consider the size of the executable, memory used for process management, and more. But on x86, which normally has lots of memory, these things are really nothing to worry about.
If the array is larger, then it takes 4 bytes per integer, and the overheads, which stay the same, become negligible. There's no padding between the integers, so for large arrays, all you should care about is the 4 bytes per integer.
这听起来像是家庭作业,所以我会问你一些问题:
This sounds like homework so I'll ask some questions of you: