使用的realloc与自由之间的差异 - >的malloc函数 [英] Differences between using realloc vs. free -> malloc functions

查看:124
本文介绍了使用的realloc与自由之间的差异 - >的malloc函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么一个会用realloc()的函数来调整一个动态分配的数组,而不是使用free()函数再次调用malloc的()函数(即利弊,优势劣势对比等)之前?这对C语言编程,但我不能找到合适的标签吧。先谢谢了。

Why would one use realloc() function to resize an dynamically allocated array rather than using free() function before calling the malloc() function again (i.e. pros and cons, advantages vs. disadvantages, etc.)? It's for C programming, but I can't find the proper tag for it. Thanks in advance.

推荐答案

的优点是,下realloc将preserve存储器中的内容。随着免费+的malloc你需要在数组中的数据重置。

The advantage is that realloc will preserve the contents of the memory. With free + malloc you'd need to reset the data in the array.

这篇关于使用的realloc与自由之间的差异 - >的malloc函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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