本文介绍了Arraym malloc()和free()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好, 我有关于malloc和free的问题。 这里我的代码示例: int main( ) { / *为数组分配动态内存* / int * array =(int *)malloc(5 * sizeof(int )); / * ...程序代码...... * / array =(int *)malloc(4 * sieof (int)); 免费(数组); } 现在我的问题:第二次我为数组分配了 内存,我用第一个malloc获得的 地址会怎么样? 是否会自动释放? MartinHello,I have a question regarding malloc and free.Here my code sample:int main(){/* allocating dynamic memory for array */int* array = (int*) malloc(5 * sizeof(int));/* ... program code ... */array = (int*) malloc(4 * sieof(int));free(array);}Now my question: The second time I allocatememory for array, what happens to theaddress that I got with the first malloc?Is it freed automatically?Martin推荐答案 编号如果您没有保存原始指针的副本,则 内存将被浪费(泄露)。 - 给我们所有人的教训:即使在琐事中也有陷阱。 --Eric SosmanNo. If you didn''t save a copy of the original pointer, thememory is wasted ("leaked").--"A lesson for us all: Even in trivia there are traps."--Eric Sosman 使用malloc()分配的内存不会自动释放,除非 ,当程序调用exit()时。 Gordon L. BurdittMemory allocated with malloc() is not freed automatically, exceptperhaps when the program calls exit().Gordon L. Burditt 废话! 只有劣质编译器无法警告 隐式声明函数`malloc''Nonsense!Only inferior compiler fail to warn aboutimplicit declaration of function `malloc'' 这篇关于Arraym malloc()和free()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 15:12