C中的动态内存分配 [英] Dynamic memory allocation in C

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

问题描述

1)如果我创建malloc()并增加大小我使用realloc()

2)如果我创建malloc(),使用free()删除内存,再次使用malloc()增加尺寸



如果我使用2方法而不是1也有缺点1和2方法之间有什么区别



我尝试了什么:



不知道是否有任何区别o缺点

1)If I create malloc() and to increase size i use realloc()
2)If i create malloc() , use free() to remove memory and again malloc() to increase the size

Did there is disadvantage if I use 2 method instead of 1 also did there any difference between 1 and 2 method

What I have tried:

no idea if there can be any difference o disadvantage

推荐答案

realloc的优点是无法编写和运行复制功能,有时应该更快,因为malloc经常使用更多的块,这些块在realloc中使用。



在程序中,最好使用复制尽可能少的内存,因为它没有任何优势。最好仔细使用指针。



正如理查德写道:写一些大循环来测试它。
The advantage of realloc is that havent to write and run the copy function and should be faster sometimes, because malloc often uses a greater chunk which got used in the realloc.

In programs it is best use to copy as little memory because there is no advantage in it. It better to work carefully with the pointers.

As Richard wrote: write some big loops to test it out.


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

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