Click here to Skip to main content
16,015,504 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Yow people! :)

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

C++
//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;
}


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?

C++
//main.cpp

#include <iostream>

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

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


Thanks!


Adjusted HTML format for coding
-Emilio-
Posted
Updated 11-Apr-11 23:38pm
v4
Comments
Nemanja Trifunovic 12-Apr-11 14:07pm    
Why does this question have MemoryBarrier tag?

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.
 
Share this answer
 
Comments
Albert Holguin 11-Apr-11 22:09pm    
correct... my 5
Ozer Karaagac 11-Apr-11 22:16pm    
Thanks, Albert...
Sergey Alexandrovich Kryukov 11-Apr-11 22:25pm    
Agreed, my 5. You can use smart pointers.
--SA
Ozer Karaagac 11-Apr-11 22:40pm    
Greetings, SA...
Espen Harlinn 12-Apr-11 15:44pm    
Nice and simple reply, my 5 :)
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.
 
Share this answer
 
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;
}

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)
 
Share this answer
 
You've already got the answer to your original question. But unless you asked this purely out of academic curiosity I've got to say that there is rarely a really good reason to return an int*. Just return an int.

Even in cases where a heap object must be used, most people will use a smart-pointer object that will manage the heap-object's destruction when it is not needed any longer.
 
Share this answer
 
Comments
Espen Harlinn 12-Apr-11 15:45pm    
Good points, 5ed!
<b>
You need not to delete this type of allocated memory(usually on Heaps)...
But you SHOULD...

When the programs ends this will be collected by operation system... so you need not to!!!
But if your app is memory-extensive... doing this kinds of memory leaks is wastage of memory... so you SHOULD free up allocated memory as your app might need it...

if this kind of allocation is done in any function of your own or provided to you...
this type of leaks should be informed about in documentation of these functions...:)
</b>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900