deleting dynamically allocated variables outside their declaring functon

Started by
14 comments, last by Iron Eye 20 years, 9 months ago
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.
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

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



find your elementat mutedfaith.com.
Advertisement
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.
I don''t know if it''s allowed, but why not create a macro instead?

.lick
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
Something like :

#define DELETE( X ) { if( X != NULL ) { delete X; X = NULL; } }

maybe?

ToohrVyk

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]
I''m American and damn proud of it.
However, it is not uncommon at all to delete objects on the heap in a different function than they were created. You have to check that you don''t create memory leaks, though.

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.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
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.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
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)
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

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



find your elementat mutedfaith.com.
Try this link to a quick and dirty tutorial on pointers, here.

HTH

This topic is closed to new replies.

Advertisement