函数返回后是否需要删除堆栈中的新指针? [英] Do i need to delete my new pointer in stack after function returns?

查看:118
本文介绍了函数返回后是否需要删除堆栈中的新指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人民! :)

请说一个字.
如果我在mySomeFunction()中使用新的指针堆栈,函数返回后是否需要删除新的pMyPointer?

Yow people! :)

One word please.
If i use new pointer stack in mySomeFunction(), does it need to delete my new pMyPointer after function returns?

//main.cpp

#include <iostream>

int myNumber();

int main()
{
   myNumber();
   std::cout << "My favorite number is: " << myNumber();

   return 0;
}

int myNumber()
{
   int * pMyPointer;
   pMyPointer = new int;

   *pMyPointer = 13;

   return *pMyPointer;
}



函数返回后,分配给我的内存堆栈是否会自动释放?或者在任何情况下都必须编写删除pMyPoiter;从下面的main()代码中的函数返回后?



Does my allocated stack of memory will be free automaticly after function returns? Or in any case it''s necessary to write delete pMyPoiter; after return from function like it''s in main() code below?

//main.cpp

#include <iostream>

int main()
{
  int * pMyPointer = new int;
  *pMypointer = 13;

  std::cout << "My favorite number is: " << *pMyPointer;
  delete pMyPointer;
  return 0;
}



谢谢!


调整了HTML格式以进行编码
-Emilio-



Thanks!


Adjusted HTML format for coding
-Emilio-

推荐答案

您需要删除new分配的每个指针,以防止内存泄漏.在裸露的C ++中,没有垃圾回收机制.
You need to delete every pointer which is allocated by new in order to prevent memory leaks. In bare C++, there is no garbage collection mechanism.


是的,如果每个新变量都必须删除,否则将发生内存泄漏

在函数中创建一个指针并在另一个函数中删除该指针也是不可行的做法,在主函数中创建指针,将其作为arg传递,然后在主函数中将其删除.
yes you must have a delete for every new if not you will have a memory leak

also its considered bad practice to create a pointer in a function and delete it in an other create the pointer in the main, pass it as an arg then delete it in the main.


最好方法是分配或创建对象的函数也会销毁它.在您给出的示例中,有两种可能的解决方案:
The best approach is that the function that allocates or creates an object also destroys it. In the example you gave, there are two possible solutions:
void MyNumber(int* pMyNumber) {
   if (pMyNumber != 0) // test to make sure the pointer is valid
      *pMyNumber = 13;
}
int main() {
   int* pMyNumber = new int;
   MyNumber(pMyNumber);
   std::cout << "My favourite number is: " << *pMyNumber << std::endl;
   delete pMyNumber; // clean up!
   return 0;
}


该解决方案将变量的分配和释放委托给调用程序.然后将创建的对象传递给设置其值的函数.


This solution delegates the allocation and freeing of the variable to the calling program. The created object is then passed to the function that sets its vale.

int* CreateMyNumber() { // ''Create'' implies we need to ''Destruct'' as well!
   int * pMyNumber = new int;
   *pMyNumber = 13;
   return pMyNumber;
}
void DestroyMyNumber(int* pMyNumber) { // cleans up whatever CreateXX() constructed
   delete pMyNumber;
}
int main() {
   int* pMyNumber = CreateMyNumber();
   std::cout << "My favourite number is: " << *pMyNumber << std::endl;
   DestroyMyNumber(pMyNumber);
   return 0;
}


该解决方案提供了分别创建和销毁某些东西的一对功能.调用函数仍然负责调用Destroy函数,以销毁Create函数构造的对象.

这是一种更为通用的解决方案,它允许Create和Destroy函数的设计者使用除new和delete之外的其他分配器,甚至可以使Create返回指向不需要销毁的静态对象的指针. (因此,销毁功能不会执行任何操作)


This solution provides a pair of function that respectively create and destroy something. The calling function is still responsible for calling the Destroy function to destruct the object that the Create function constructed.

This is a more generic solution that allows the designer of the Create and Destroy functions to use other allocators than new and delete, or maybe even have Create return a pointer to a static object that doesn''t need to be destroyed. (so the Destroy function would do nothing)


这篇关于函数返回后是否需要删除堆栈中的新指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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