By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,902 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,902 IT Pros & Developers. It's quick & easy.

Local Pointers

P: n/a
If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
*/

return aux;

}

Does the program destroy the pointers after exiting the function or
should I delete them anyway at the end of the function?

Nov 2 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Gaijinco wrote:
If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
Supposedly, filling 'aux' with a valid pointer value...
*/

return aux;

}

Does the program destroy the pointers after exiting the function or
should I delete them anyway at the end of the function?


Returning a value of a pointer is orthogonal to any other action, if by
the time you return the value (and after the function exits) the value is
still valid.

The program destroys _every_ object with automatic storage duration upon
leaving the scope in which they are defined. So, yes, the pointers are
destroyed. Destroying a pointer, though, does NOT mean deleting the
object to which that pointer points. The decision whether to delete or
not to delete something is also completely orthogonal to the rest of the
stuff here. If you don't need the object any more, delete it (through
a pointer to it). If you need the object to survive longer, don't delete
it.

V
Nov 2 '05 #2

P: n/a
Gaijinco wrote:
If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
*/

return aux;

}

Does the program destroy the pointers after exiting the function
Yes. aux, p and q have automatic storage duration so are destroyed when
they go out of scope. In your case that's at the end of the function.
But the rest of your question suggests that you may be confusing two
issues.
or should I delete them anyway at the end of the function?


Read this
http://www.parashift.com/c++-faq-lit....html#faq-16.1

So what you really need to know is, should you delete the memory
pointed to the pointers? To learn that, read the rest of section 16 in
the FAQ.

http://www.parashift.com/c++-faq-lit...tore-mgmt.html

After you've read section 16, you should be left with an impression
that managing pointers and dynamic memory is hard. It is. So have a
look at

http://www.parashift.com/c++-faq-lit....html#faq-34.1

and the rest of the FAQ.

Gavin Deane

Nov 2 '05 #3

P: n/a

On 2 Nov 2005 06:33:14 -0800
"Gaijinco" <ga******@gmail.com> wrote:
If I do something like:

int* Foo(){

int *aux, *p, *q;

/*
NIFTY CODE HERE
*/

return aux;

}

Does the program destroy the pointers after exiting the function or
should I delete them anyway at the end of the function?

Think about
char *a = "abc";
char a[] = "abc";

Nov 2 '05 #4

P: n/a
> Think about
char *a = "abc";
char a[] = "abc";


What about it?

Thanks for the replies! I now understand that the pointers are
destroyed after the function exits, but of course the ponitees are not.
For a while I confused

delete ptr;

with

ptr = NULL;

But things are much clear now.

Thanks again!

Nov 2 '05 #5

P: n/a
lpw
> > Think about
char *a = "abc";
char a[] = "abc";


What about it?


#include <iostream>

using namespace std;

char *f1() {
char *a = "abc";
return a; // return pointer to statically allocated string constant
}

char *f2() {
char a[] = "abc";
return a; // return address of a local array
}
int main(void) {
cout << f1() << endl; // ok
cout << f2() << endl; // hell breaks loose
}
Nov 2 '05 #6

P: n/a

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Think about
char *a = "abc";
char a[] = "abc";
What about it?


For clarity, I'll change one name:

char * func()
{
char *a = "abc";
char b[] = "abc";

return a; /* OK */
return b; /* ERROR */
}

Since the string pointed to by 'a' is a literal,
its lifetime is that of the program (it's not
destroyed until termination). The array 'b's
lifetime ends when the function returns (it's
an 'auto' object, so it gets destroyed.)

So returning 'a' to the caller gives the caller
access to the literal. But returning 'b' (the
array's name, which will be converted to a pointer
to its first element), would give the caller the
address of an object that has just been destroyed.

Thanks for the replies! I now understand that the pointers are
destroyed after the function exits, but of course the ponitees are not.
That depends upon where the 'pointees' live.

void f1()
{
int i = 0;
int *p = &i;
return; /* both 'i' (the pointee) and
'p' (the pointer) are destroyed */
}

void f2()
{
int *p = new int;
return; /* 'p' is destroyed, but the object created
by the 'new' operator is not */
/* thus, this function would create a memory leak, since
it does not preserve the value of 'p' or pass it to
anywhere else */
}

} For a while I confused

delete ptr;

with

ptr = NULL;
Not a good thing. :-)

But things are much clear now.


IMO it's actually rather rare to really need to
use 'new' and 'delete' directly. The mechanisms of auto
storage class, and the standard library containers
which handle their own memory management cover
most cases.

-Mike
Nov 2 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.