Archived

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

Dumb pointer question

This topic is 5568 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 thought I was beginning to understand pointers until I read a line in one of my books. It said that you have to remember that although the memory on the free store that the pointer points to will not go away when a function returns...the pointer itself IS a local variable and will go away when the function returns. Huh??? I thought the whole purpose of functions was so that you could pass by reference into functions and to create a variable that would have a scope that exists outside of the function itself. But if the pointer is just a local variable itself, once the function is returned, how do I get access to the area on the free store that the pointer pointed to? The only way I can see around this is to declare all pointers within main() since main() doesn''t return until the program ends. Also, wouldn''t I have to call delete on the pointer to avoid a memory leak?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A pointer is just like any other variable type, it''s value just happens to hold a memory address. If you allocate memory inside of a function and need to access it after the function returns, you can simply return the pointer''s value:

int* Allocate20Ints (void)
{
int* ptr = new int[20];

return ptr;
}

int* intArray = Allocate20Ints ();

You can also store the value of the pointer by reference:

void Allocate20Ints (int** dest)
{
int* ptr = new int[20];

*dest = ptr;
}

int* intArray;

Allocate20Ints (*intArray);

As you mentioned, you can also use a global variable:

int* g_intArray;

void Allocate20Ints (void)
{
g_intArray = new int[20];
}

And finally, yes you have to call delete on a pointer to release whatever you allocated to it.

Share this post


Link to post
Share on other sites
quote:
Original post by Dauntless
It said that you have to remember that although the memory on the free store that the pointer points to will not go away when a function returns...the pointer itself IS a local variable and will go away when the function returns.





  

void myfunc()
{
char * superstring = new char[10];

strcpy(superstring,"super");
}

;

in this example, the pointer superstring is pointing to an array of char. But the pointer itself is local to the function. So when the function returns, the pointer will be discarded, and memory will leak.

Share this post


Link to post
Share on other sites

if the only pointer variable that points to a memory area lives on the stack, that is, it''s an local variable, it will of course take it''s value (the address of the memory area) down the stream of oblivion when it ceases existence upon return from the function body.

So, I suppose, if in a function a pointer variable is assigned the address of a newly created object on the free store, and you don''t want to delete this object within the function body, then you should its value to the outside, either by returning it or by assigning its value to a global var, if you like those.


class C {};

C* f() {
C* c=new C;
return f_c;
} // or, simpler, just "return new C;"

int main() {
C* main_c=f();
// now f_c is dead and lost but it''s value lives
// happily on in main_c
delete main_c; // now everything''s fine again
}


after all, the typical internal representation of a pointer is just a more or less disguised number. And numbers don''t cease to exist just because a variable holding them does. Or would you expect the number "5" to cease existence as an onject of our imagination, as a symbol, as a mathematical number, when the following function returns?

void f() {
int i=5;
}

True, the variable i will be lost forever, but it''s value can live without it. Just call this function in a program, compile and run it. Then count the fingers of your hand; I''m quite certain that the number "5" will still exist and be available and ready to be used.

If not, that would be quite spooky, wouldn''t it?

Pointers are no more complicated. Only when you lose the value, how could you find it again? After all, it''s normally a huge number of practically random value that will normally be completely different the next time you create an object on the free store.


happily sharing her wisdom with the mortal,

shadi

Share this post


Link to post
Share on other sites
okay...I think I get it. I''m really going to make a pointer to a pointer? And I also need to make a function that returns a pointer, so I can call the function so that I can get access to the pointer, which in turn creates the object on the freestore.


Hmmm, I''m going to play around with this for a bit

Thanks for the help

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This is the same AP as above, and there''s a slight error in the code I posted (d''oh!).

int* intArray;

Allocate20Ints (*intArray);

Should be:

int* intArray;

Allocate20Ints (&intArray);

Share this post


Link to post
Share on other sites