动态内存碎片C [英] Dynamic memory fragmentation C

查看:57
本文介绍了动态内存碎片C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管存在性能问题,我目前还没有在动态分配的内存块中使用指针算法:

Despite the performances issues, I currently don't use the arithmetic of pointers in the block of memory allocated dynamically such:

int* foo =malloc(sizeof(int)*100);
     *(foo+1)=100; //I do not use this form
     foo[1]=100;//but this



这是因为我害怕内存可能碎片化。这随机可能会粉碎并引发臭名昭着的SIGSEV。

但我的恐惧是否合理?我可以使用指定内存的指针算法吗?



我尝试过:



我无法尝试,因为问题来自程序分配内存的方式


this because I am afraid of the possible fragmentation of the memory. That randomly could crush and raise an infamous SIGSEV.
But are my fears justified? Can I use the arithmetic of pointers with allocated memory?

What I have tried:

I cannot try, since the problem comes from the way the program allocated the memory

推荐答案

方法之间没有区别。编译器通常会为这两种方法生成相同的汇编代码。



这也与内存碎片无关。当频繁分配和释放内存时会发生堆内存碎片,这可能导致未使用的释放块。



获取分段错误是访问内存超出范围的结果。对于您的示例,当使用负索引或大于或等于100的索引或者 - 甚至更糟 - 使用带有不同类型的转换指针时会发生这种情况。因为C没有实现绑定检查,所以必须通过编写相应的代码来确保它不会发生。



如果你有分段错误,你必须检查和调试你的代码以找到源。
There is no difference between the methods. The compiler will usually generate the same assembly code for both methods.

This is also not related to memory fragmentation. Heap memory fragmentation occurs when allocating and freeing memory frequently which may result in unused freed blocks.

Getting a segmentation fault is the result of accessing memory out of bounds. For your example this happens when using a negative index or an index greater or equal to 100 or - even worse - using casted pointers to a differnt type. Because C has not implemented bound checking, you must do it by writing corresponding code to ensure that it can not happen.

If you got segmentation faults you have to check and debug your code to find the source.


不,你的恐惧是不合理的。无论你使用 *(foo + 1)还是 foo [1] ,它们都会产生确切的结果相同的目标代码。
No, your fears are not justified. It does not matter whether you use *(foo+1) or foo[1], they both result in exactly the same object code.


但我的恐惧是否合理?

编号甚至没有一点!

内存碎片不是由你访问内存的方式引起的,它是由分配和释放内存块引起的,这样就会出现许多较小的,无用的漏洞,这些漏洞不能被重用为更大的块,因为它们是不是连续的。

这里有一个很好的描述: Developer Library& amp; ; RAQUO;内存碎片,你最糟糕的噩梦 [ ^ ] - 它有图片,没有图片真的,真的很难解释发生了什么!



但是通过指针算法访问你的指针不会与碎片有关,因为它不会导致任何新的内存分配或将任何内存返回池 - 这些必须在C中通过malloc显示并且是免费的
"But are my fears justified?"
No. Not even slightly!
Memory fragmentation is not caused by how you access the memory, it's caused by allocating and freeing chunks of memory in such a way that lots of smaller, useless holes appear that can't be reused as a "bigger chunk" because they aren't contiguous.
There is a good description here: Developer Library » Memory Fragmentation, your worst nightmare[^] - and it has pictures, without which it's really, really difficult to explain what is happening!

But accessing your pointers via pointer arithmetic doesn't have anything to do with fragmentation, as it doesn't cause any new allocation of memory or return any memory to the pool - those have to be explicit in C via malloc and free


这篇关于动态内存碎片C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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