变量是否自动释放C/C ++中的内存 [英] Is the variable automatically free memory in C/C++

查看:119
本文介绍了变量是否自动释放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"变量分配给 0x00000000 存储器地址,而"arr"分配给 0x00000004 存储器地址.
在到达"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 0x00000000 memory address and "arr" is allocated to the 0x00000004 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

推荐答案

规则1,您要分配的任何malloc()都必须由free()''处理,没有任何魔术可以做到这一点.你.

规则2,局部变量(在您的情况下为i,j和arr)通常在堆栈上本地存储.当您的例行程序退出时,它们就会消失.

规则3,堆栈会弄脏您正在执行的其他操作的调用/局部变量,因此,如果您自己没有初始化堆栈,则不能依赖它们的内容.不能依靠"i"和"j"在堆栈上占据相同位置这一事实来提供有关剩余变量"的任何有意义的信息.

规则4,编译器可以自由地优化"局部变量.因此,编译器可能会确定"j"实际上可以是处理器寄存器,并且最终根本不占用任何内存或堆栈.

规则5,当您摆脱简单的语言类型(如"char"或"int")并开始使用C ++类时,作为类实例化的局部变量会在函数退出时(从技术上讲,当变量退出时)调用其析构函数超出范围).那是唯一的魔术,可以帮助编译器为您提供帮助.
Rule 1, anything you malloc() has to be free()''ed by you, there is no magic that will do that for you.

Rule 2, local variables (in your case, i, j, and arr) are stored locally, usually on the stack. They disappear when your routine exits.

Rule 3, stacks tend to get dirty with calls / local variables of other things you are doing so you cannot rely on their contents if you did not initialize it yourself. The fact that ''i'' and ''j'' may have occupied the same location on the stack cannot be relied on provide any meaningful information about the "leftover variables".

Rule 4, compilers are free to ''optimize away'' local variables. So, the compiler may decide that ''j'' can really be a processor register and end up occupying no memory or stack at all.

Rule 5, when you get away from simple language types like ''char'' or ''int'' and start using C++ classes, local variables that are instantiations of classes have their destructor called when the function exits (technically, when the variable goes out of scope). That''s about the only magic and help the compiler gives you.


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

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