Jump to content
  • Advertisement

Archived

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

Iron Eye

deleting dynamically allocated variables outside their declaring functon

This topic is 5489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!