Question about scope of variables created with new

A

africamp

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
 
R

Rolf Magnus

africamp said:
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

Of course. Just do:

delete b;

within main.
 
A

africamp

but doesn't that just deleter the pointer to the integer and not the
integer itself?
 
V

Victor Bazarov

africamp said:
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
 
A

Alan Brown

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
 
D

Dave Vandervies

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

Rolf Magnus

Alan said:
...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.
 
R

Rolf Magnus

africamp said:
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.
 
S

snnn

africamp said:
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[].
 
R

Ron Natalie

Dave said:
...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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top