Archived

This topic is now archived and is closed to further replies.

Iron Eye

deleting dynamically allocated variables outside their declaring functon

Recommended Posts

I'll just give an example of what I mean.
void DeletePointer(int& pointer); //could template this
int main()
{
     int* yum = new int;
     *yum = 50;
     DeletePointer(yum);
}
DeletePointer(int& pointer) //are references to points even possible?
{
     delete pointer;
     pointer = 0; //make it point to nothing
}
   
If this is allowed (I'm about to test it), is it good coding practice to do so? ---
ConPong  _//_  Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me

--- EDIT:
#include <iostream>
using namespace std;

void DeletePointer(int* pointer); //could template this
int main()
{     
    int* yum = new int;     
    *yum = 50;     
    DeletePointer(yum);
    yum = new int[4];
    *(yum + 2) = 5; 
    cout << *(yum + 2) << endl;
    system("pause");
    return 0;
}
void DeletePointer(int* pointer)
{     
    delete pointer;     
    pointer = 0; //make it point to nothing
}
  
This seems to work, I want someone who knows what they are doing to double check it though.

Share this post


Link to post
Share on other sites
Just so long as you don''t forget to delete them

Whatever works best for you is the method you should choose. I''m still a newbie though so someone more experienced should be able to give you some better advice.

Share this post


Link to post
Share on other sites
Creating a function to delete a pointer is pointless (no pun intended). It''s much easier to just delete the memory once you''re done with it. And there''s no need to set the pointer to zero afterwards as if you''re going to reuse it, the call to new will overwrite its contents anyway.

HTH

Share this post


Link to post
Share on other sites
im gonna use this as a great chance to ask about pointers like a noob...when u ask for the adress...u know (*..or is it &?...i dunno thats another question i guess) well...does that mean the value is an octal adress of the number in physical memory (maybe i dont know wut im talkin about....but im tryin here) like....Ox00016 or somethin like that...i really dont know (and i use "..." too much) if anyone can make sense of my rambling and help..thatd be cool

[edited by - Ademan on July 6, 2003 3:35:33 PM]

Share this post


Link to post
Share on other sites
Ademan: In memory, it is stored, of course, binary. (A normal integer is, too.) However, if, for some reason, an address needs to be displayed, normally the hexadecimal system is chosen, which is probably what you mean.

Hexadecimal numbers are prefixed by 0x (the number 0, not the letter O). Their digits can include 0-9 and A-F.
Octal numbers'' digits range from 0-7. They are prefixed with a (number) 0. You shouldn''t prefix decimal literals with 0 because they''re going to be interpreted as octal.


My Wonderful Web Site (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

Share this post


Link to post
Share on other sites
I actually do not intend to reuse the pointer in my real program.
I was trying to see if there was a memory leak by reusing it. However, ints are so small I couldn''t tell in the task manager.

I was merely trying to cut down the number of lines of code it took to delete a pointer. (1 line versus 2 lines to point it to zero)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


To read a declaration, read from the variable name, first to the right, then to the left, and make parentheses matter. Thus:


void * func(); // func is a function returning pointer to void
void (*func)(); // func is a pointer to a function returning void
int & * ptr; // ptr is a pointer to a reference to an int, which is illegal
int * & ptr; // ptr is a reference to a pointer to an int, which is fine

inline void DeletePointer( int * & ptr )
{
delete ptr;
ptr = 0;
}


Or, more flexibly:


template< class T >
void DeletePointer( T * & ptr )
{
delete ptr;
ptr = 0;
}


Of course, if you allocate with new[], you have to delete with delete[].

Share this post


Link to post
Share on other sites
To answer the OP’s question, you can delete dynamically created variables anywhere in your code where the pointer is visible. But you should know the scope of your dynamically created objects and it is good practice to create and delete things in the context of that scope.

For example if you create an array of integers at the beginning of your main function you should be deleting them at the end of main. If you are using new in the constructor of an object, you should be deleting them in the destructor.

It’s more of a programming preference/style thing than something you have to do, but your code will probably be more readable if you follow such a convention.

PS. Iron Eye, you will have memory leak in the second piece of code you posted.



[edited by - KiwiMelon on July 6, 2003 8:29:18 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ToohrVyk
Something like :

#define DELETE( X ) { if( X != NULL ) { delete X; X = NULL; } }
The comparison to NULL is redundant, as delete-ing a null pointer is guaranteed to do nothing.

Share this post


Link to post
Share on other sites
quote:
Original post by IvanTheTerrible
And there''s no need to set the pointer to zero afterwards as if you''re going to reuse it, the call to new will overwrite its contents anyway.

HTH


Setting the deleted pointer to zero is a very good practice, IMO. It makes it impossible for your program to try to access that memory later (which would lead to a pretty hard-to-find bug). It''s pretty common practice to do so.

Iron Eye, the code you posted will work fine, but most people just use a macro for this thing, like:
#define DELETE( X ) { delete X; X = NULL; }

Share this post


Link to post
Share on other sites
quote:
Original post by andy_fish
[Setting the deleted pointer to zero] makes it impossible for your program to try to access that memory later...
...so does letting the pointer go out of scope.

Share this post


Link to post
Share on other sites
Beer hunter: sorry, I got too much used to smart pointers, with reference count and such... where you need to do :

if( P != NULL ) { P->Release( ); P = NULL; }

Besides, adding a "if( P != NULL )" condition each time you access a pointer makes it indeed impossible for the program to use a pointer that was deleted, whereas you can''t if you don''t set the pointer to NULL.

ToohrVyk

Share this post


Link to post
Share on other sites