Archived

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

nosajghoul

pointer out of scope = deleted?

Recommended Posts

Hi all, Compare the following to understand my confusion : bool somefunction() { int x = 5; blahblahblah.... return true; } Ok, int x has been removed from the stack and no longer takes up memory after the function returns, right? bool somefunction() { int *pInt = new int; blahblahblah return true; } Is pInt removed from the stack after this function returns, like int x was? In other words, do I still need to delete it? My confusion lies in the question of if you have to manage dynamic memory after it goes out of scope. Also, what if you never used the new keyword in the second function, but just gave it some address? bool somefunction() int *pInt = 0; int a = 5; pInt = a; blahblahblah.... return true; } What happens when this goes out of scope, compared to my second example? later, Jason PS I think the sensation of funny occurs when your brain misplaces a pointer...

Share this post


Link to post
Share on other sites
When you use a pointer to allocate some memory, you always need to delete it to free up the memory or else you will have a memory leak.

Share this post


Link to post
Share on other sites
A simple rule of thumb - everything you allocate, you must deallocate. Every 'new' should have a matching 'delete', every new[] should have a matching 'delete[]', and every 'malloc/calloc' should have a matching 'free'. Simply assigning an address to a pointer using the & operator does not allocate memory, so it's not something to worry over.

[edited by - aldacron on March 21, 2004 10:03:54 AM]

Share this post


Link to post
Share on other sites

bool somefunction()
{
int *pInt = new int;
// Do stuff with our new integer

return true;
}


Classic memory leak. You need to call delete pInt before you return from the function.


bool somefunction()
{
int *pInt = 0;
int a = 5;
pInt = a;
return true;
}


That code won''t actually do too much. When you want to assign an address to a pointer of a regular static variable (like int a) you need to call it as follows: pInt = &a; Either that, or when you init pInt you do it as follows: int *pInt = new int; And then you can assign a value into it, like *pInt = a;
If a were a pointer to an integer as well then calling pInt = a; is fine.

Share this post


Link to post
Share on other sites
"Simply assigning an address to a pointer using the & operator does not allocate memory, so it''s not something to worry over"

I have always wondered this, thank you so much for setting the record straight.

Share this post


Link to post
Share on other sites
quote:
Original post by nosajghoul
Hi all,

Is pInt removed from the stack after this function returns, like int x was?



Yes, it is. What makes pInt different is that it's a variable that holds an address instead of an integer. The variable is cleaned up when the function returns, but any memory located at the address it stored is not.

quote:
Original post by nosajghoul
do I still need to delete it?



Yes, like Aldacron said, if you call any of the memory allocation functions you'll need to call the corresponding deallocation function. However using the & operator IS something you need to be carefull with. Consider the following:


bool Example()
{
int *pInt = NULL; // create an int pointer

{
int n = 5;
pInt = &n; // pInt now stores the address of the n variable

}
// n goes out of scope and is removed from the stack,

// but pInt still points to the address where it use to be.


cout << *pInt; // <- error, pInt points to unallocated memory

return true;
}


edit: Oops, didn't see the other post where you seem to have caught on to all of this.


[edited by - Solo on March 21, 2004 3:08:53 PM]

Share this post


Link to post
Share on other sites