本文介绍了变量是否自动释放C/C ++中的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的ansi C代码示例:

I have my following ansi C code example:

void Example1()
{
 int i;
 int *arr = (int*)malloc(5*sizeof(int)); // int arr[5];

 for (i=0;i<5;i++)
   arr[i]=i;
 //do something
 //...
}

void Example2()
{
 int j;
 j=0;
 //do some thing
 //...
}

void main()
{
 Example1();
 Example2();
}



程序运行时,首先调用"Example1"功能,然后再调用"Example2"功能.

大概,当"Example1"中的已编译程序时,"i"变量分配给存储器地址,而"arr"分配给存储器地址.
在到达"Example1"中的行尾并转到"Example2"后,启动过程来分配给"j"变量.现在,停止它.我有以下一些问题,真的需要你们帮助我.

"j"变量的确切地址位置在哪里?
在主程序中完成任务后,"Example1"是否自动释放"i"和"arr"变量的内存?
每个特定的编译器都有一种使用或不使用后自动释放内存的机制? (或依靠什么?)

预先感谢大家



when the program run, the "Example1" function is called first and then followed the "Example2" function.

Assumaply, when the compiled program in the "Example1", "i" variable is allocated to the memory address and "arr" is allocated to the memory address.
After passing through to the end of line in "Example1" and go to the "Example2" and starting process to allocated to the "j" variable. Now, stop it. I have some following questions and really need you guys to help me out.

Where exactly address location of "j" variable is?
Is the "Example1" after completing its mission in main program automatically free the memory of "i" and "arr" variable?
Every specific compiler have a mechanism automatically free the memory after using or not? (or depend on something?)

Thanks guys in advance

推荐答案


这篇关于变量是否自动释放C/C ++中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:52