微妙的内存泄漏,这是常见的做法吗? [英] Subtle Memory Leak, and is this common practice?

查看:130
本文介绍了微妙的内存泄漏,这是常见的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可能在这里创建一个内存泄漏:

  void commandoptions(){
cout< ; 你有以下选项:\\\
1)。购买Something.\\\
2)。检查你的余额。\\\
3)。看看你买了什么。\\\
4。)离开store.\\\
\\ \\ n输入数字以作出选择:;
int input;
cin>>输入;
if(input == 1)buy();
//继续选项列表.....
else
commandoptions(); //如果你删除了ELS语句,内存泄漏!
}

inline void buy(){
//购买某物
commandoptions();
}

让我们说commandoptions刚刚在程序第一次运行时执行。用户选择'1',意味着通过commandoptions()子程序执行buy()子程序。



执行后,它再次调用commandoptions



第一个commandoptions()是否返回?



如果我创建一个子程序,只是调用自己,它会导致 stackoverflow ,因为那个子程序的其他'周期'永远不会退出。



请注意,我使用 inline 关键字在购买...这有什么区别吗?



我很高兴地问我的教授,他似乎没有。 :/



EDIT :我不能相信 但是感谢我对我的术语学到了新的东西。

解决方案

内存泄漏是你使用 new 如下:

  char * memory = new char [100] //分配100字节

然后你忘记,使用这个内存后删除内存

  //将使用的内存返回系统。 

如果您忘记了 delete 在程序运行时将此内存保留为正在使用,并且无法重复使用其他内容。看到内存是一个有限的资源,做这个数百万次,例如,没有程序终止,将结束你没有留下使用的内存。



这是为什么我们自己清理后的原因。



在C ++中,防止内存泄漏。

  class RAII 
{
public:
RAII = new char [100]; }
〜RAII(){delete [] memory}
//其他函数做的东西
private:
char * memory;
};

现在您可以使用此RAII类,因为

  {//某种范围
RAII r; //分配一些内存

//使用r

执行操作} //结束范围销毁r并调用析构函数,删除内存



您的代码不会显示任何内存分配,因此没有可见的泄漏。



你的代码似乎有无尽的递归,没有一个基本情况将会终止递归。


I think I might be creating a memory leak here:

   void commandoptions(){
      cout<< "You have the following options: \n 1). Buy Something.\n  2).Check you balance. \n3). See what you have bought.\n4.) Leave the store.\n\n Enter a number to make your choice:";
      int input;
      cin>>input;
      if (input==1) buy();
      //Continue the list of options.....
      else
      commandoptions(); //MEMORY LEAK IF YOU DELETE THE ELSE STATEMENTS!
   }

inline void buy(){
    //buy something
    commandoptions();
}

Let's say commandoptions has just exectued for the first time the program has been run. The user selects '1', meaning the buy() subroutine is executed by the commandoptions() subroutine.

After buy() executes, it calls commandoptions() again.

Does the first commandoptions() ever return? Or did I just make a memory leak?

If I make a subroutine that does nothing but call itself, it will cause a stackoverflow because the other 'cycles' of that subroutine never exit. Am I doing/close to doing that here?

Note that I used the inline keyword on buy... does that make any difference?

I'd happily ask my professor, he just doesn't seem available. :/

EDIT: I can't believe it didn't occur to me to use a loop, but thanks, I learned something new about my terminology!

解决方案

A memory leak is where you have allocated some memory using new like so:

char* memory = new char[100]; //allocate 100 bytes

and then you forget, after using this memory to delete the memory

delete[] memory; //return used memory back to system.

If you forget to delete then you are leaving this memory as in-use while your program is running and cannot be reused for something else. Seeing that memory is a limited resource, doing this millions of times for example, without the program terminating, would end you with no memory left to use.

This is why we clean up after ourselves.

In C++ you'd use an idiom like RAII to prevent memory leaks.

class RAII
{
public:
    RAII() { memory = new char[100]; }
    ~RAII() { delete[] memory }
    //other functions doing stuff
private:
   char* memory;
};

Now you can use this RAII class, as so

{ // some scope
RAII r; // allocate some memory

//do stuff with r

} // end of scope destroys r and calls destructor, deleting memory

Your code doesn't show any memory allocations, therefore has no visible leak.

Your code does seem to have endless recursion, without a base case that will terminate the recursion.

这篇关于微妙的内存泄漏,这是常见的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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