| View previous topic :: View next topic |
| Author |
Message |
africamp Guest
|
Posted: Fri Feb 25, 2005 1:05 am Post subject: Question about scope of variables created with new |
|
|
Lets say I have a program:
#include <iostream>
#include <conio.h>
using namespace std;
int* ptest();
int main()
{
int* b = ptest();
getch();
return 0;
}
int* ptest()
{
int num = 5;
int* intp = new int(num);
return intp;
}
Is there any way to free the memory that was allocated for the variable
intp outside of the function, ptest()? Thanks
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Feb 25, 2005 1:16 am Post subject: Re: Question about scope of variables created with new |
|
|
africamp wrote:
| Quote: | Lets say I have a program:
#include
#include
using namespace std;
int* ptest();
int main()
{
int* b = ptest();
getch();
return 0;
}
int* ptest()
{
int num = 5;
int* intp = new int(num);
return intp;
}
Is there any way to free the memory that was allocated for the variable
intp outside of the function, ptest()? Thanks
|
Of course. Just do:
delete b;
within main.
|
|
| Back to top |
|
 |
africamp Guest
|
Posted: Fri Feb 25, 2005 1:17 am Post subject: Re: Question about scope of variables created with new |
|
|
but doesn't that just deleter the pointer to the integer and not the
integer itself?
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Feb 25, 2005 1:45 am Post subject: Re: Question about scope of variables created with new |
|
|
"africamp" <africamp (AT) gmail (DOT) com> wrote...
| Quote: | but doesn't that just deleter the pointer to the integer and not the
integer itself?
|
No, 'delete' destroys the object, then deallocates the memory that was
used for that object. The pointer value is used to tell the 'delete'
where to find the object and the memory. The pointer itself survives.
V
|
|
| Back to top |
|
 |
Alan Brown Guest
|
Posted: Fri Feb 25, 2005 2:52 am Post subject: Re: Question about scope of variables created with new |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in
news:K_mdnSZb587PHoPfRVn-gQ (AT) comcast (DOT) com:
| Quote: | "africamp" <africamp (AT) gmail (DOT) com> wrote...
but doesn't that just deleter the pointer to the integer and not the
integer itself?
No, 'delete' destroys the object, then deallocates the memory that was
used for that object. The pointer value is used to tell the 'delete'
where to find the object and the memory. The pointer itself survives.
|
....and should be reset to NULL for safety
Alan
|
|
| Back to top |
|
 |
Dave Vandervies Guest
|
Posted: Fri Feb 25, 2005 3:25 am Post subject: Re: Question about scope of variables created with new |
|
|
In article <Xns96088CE765A85416c616e (AT) 211 (DOT) 29.133.50>,
Alan Brown <rigelau (AT) yahoo (DOT) nospam.com.au> wrote:
| Quote: | "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in
news:K_mdnSZb587PHoPfRVn-gQ (AT) comcast (DOT) com:
"africamp" <africamp (AT) gmail (DOT) com> wrote...
but doesn't that just deleter the pointer to the integer and not the
integer itself?
No, 'delete' destroys the object, then deallocates the memory that was
used for that object. The pointer value is used to tell the 'delete'
where to find the object and the memory. The pointer itself survives.
...and should be reset to NULL for safety
|
....even though it doesn't gain you much safety, unless you can be
absolutely certain that it's the only pointer to the just-deleted object
that exists.
But it only "survives" in that the pointer object[1] still exists and
you can point it at something else - the actual value of the pointer
can no longer be safely used.
dave
[1] This is "object" in the C sense of "region of storage containing a
value", not in the OO sense of "thing with state and behavior".
--
Dave Vandervies [email]dj3vande (AT) csclub (DOT) uwaterloo.ca[/email]
ERT is correct. (For reasons I won't go into, this is worth pointing out.)
--Keith Thompson in comp.lang.c
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Fri Feb 25, 2005 9:43 am Post subject: Re: Question about scope of variables created with new |
|
|
"Alan Brown" <rigelau (AT) yahoo (DOT) nospam.com.au> skrev i en meddelelse
news:Xns96088CE765A85416c616e (AT) 211 (DOT) 29.133.50...
| Quote: | "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in
news:K_mdnSZb587PHoPfRVn-gQ (AT) comcast (DOT) com:
"africamp" <africamp (AT) gmail (DOT) com> wrote...
but doesn't that just deleter the pointer to the integer and not the
integer itself?
No, 'delete' destroys the object, then deallocates the memory that was
used for that object. The pointer value is used to tell the 'delete'
where to find the object and the memory. The pointer itself survives.
...and should be reset to NULL for safety
|
This is only rarely correct.
/Peter
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Feb 25, 2005 10:04 am Post subject: Re: Question about scope of variables created with new |
|
|
Alan Brown wrote:
| Quote: | "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in
news:K_mdnSZb587PHoPfRVn-gQ (AT) comcast (DOT) com:
"africamp" <africamp (AT) gmail (DOT) com> wrote...
but doesn't that just deleter the pointer to the integer and not the
integer itself?
No, 'delete' destroys the object, then deallocates the memory that was
used for that object. The pointer value is used to tell the 'delete'
where to find the object and the memory. The pointer itself survives.
...and should be reset to NULL for safety
|
Actually, I think it shouldn't in most places. If you have an erroneous
double-deletion, it will still be if you set the pointer to NULL. You just
happened to cure the symptoms, not the disease.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Feb 25, 2005 10:08 am Post subject: Re: Question about scope of variables created with new |
|
|
africamp wrote:
| Quote: | but doesn't that just deleter the pointer to the integer and not the
integer itself?
|
No. You should read up in your book about memory management. It works a lot
different from what you think. delete takes a pointer to a dynamically
allocated object and destroys that object. You cannot manually destroy
local variables (like your pointer) at all.
|
|
| Back to top |
|
 |
snnn Guest
|
Posted: Fri Feb 25, 2005 1:36 pm Post subject: Re: Question about scope of variables created with new |
|
|
"africamp" <africamp (AT) gmail (DOT) com> writes:
| Quote: | int* ptest()
{
int num = 5;
int* intp = new int(num);
return intp;
}
Is there any way to free the memory that was allocated for the variable
intp outside of the function, ptest()? Thanks
|
Of course,just to
delete intp;
However,if the memory was allocated by you but another one should
free it.You MUST write a document to told him that the return value of
ptest() point to a int value, NOT int[].
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Feb 25, 2005 3:24 pm Post subject: Re: Question about scope of variables created with new |
|
|
Dave Vandervies wrote:
| Quote: | ...and should be reset to NULL for safety
...even though it doesn't gain you much safety, unless you can be
absolutely certain that it's the only pointer to the just-deleted object
that exists.
But it only "survives" in that the pointer object[1] still exists and
you can point it at something else - the actual value of the pointer
can no longer be safely used.
|
And setting it to NULL keeps you from inadvertantly using the invalid
poitner. Unless they are going to be immediately destroyed, I don't
like leaving objects around that have indeterminate contents that will
cause undefined behavior if used.
|
|
| Back to top |
|
 |
|