destroy primitive/object types but memory is not freed

G

george972

Hello,

This is a simple question for you all, I guess .
int main(){
double *g= new double;
*g = 9;
delete g;
cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<"
"<<endl;
*g = 111;
cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<"
"<<endl;
return 0;
}

The output:
4 8 8 9
4 8 8 111

Although I delete g, why is it that I can still use it and it
references to
actual memory?

The same happens when creating and deleting object types with new and
delete!

Please dont answer with what you think but with what actually happens.
If
you can point me to web sources I can read on this, it would have been
great!

Thank you in advance.
Regards,
 
K

Keith Thompson

This is a simple question for you all, I guess .
int main(){
double *g= new double;
*g = 9;
delete g;
cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<"
"<<endl;
*g = 111;
cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<"
"<<endl;
return 0;
}

That's C++, not C. They're two different languages
with their own newsgroups. Your question would be more
suited to comp.lang.c++. (Check the FAQ first; it's at
<http://www.parashift.com/c++-faq-lite/>, and it may already answer
your question.)

If you're interested in a C answer, please re-write your program in C,
using malloc() and free() rather than new and delete -- but first
check the C FAQ at <http://www.c-faq.com/>, particularly section 7.
(I'm fairly sure that C++'s new/delete and C's malloc()/free() behave
similarl in this respect, but there might be subtle differences.)
 
G

george972

Right you are brother, substitute malloc/free if you like, it's just
the same...

Regards,
 
K

Keith Thompson

Right you are brother, substitute malloc/free if you like, it's just
the same...

How do you know it's "just the same"? If you don't know the rules for
C++, how do you know that the rules for C are the same?

In C, passing a pointer value to free() causes the memory it pointed
to to be deallocated. It doesn't set the pointer to NULL (it can't),
and it doesn't necessarily do anything in particular with the block of
memory. So if you still have a copy of the pointer, you can try to
dererence it. The behavior of any such attempt is undefined. That
doesn't mean your program's going to crash, it means that anything can
happen. More precisely, it means that anything that actually does
happen doesn't violate the C standard.

It's very likely that the physical memory is still there and hasn't
been modified, and that if you're *unlucky*, you can still access
whatever was there before you called free(). But the system is free
to re-use that memory for other things and/or to make it inaccessible.

It's your job as a programmer to avoid accessing memory after you've
free()ed it. If you go ahead and try it anyway, the implementation
isn't obliged to help you by preventing it or by diagnosing the error.
 
J

Joachim Schmitz

Joe said:
Good point. I'm driving on the right side.

Hopefully everyone drives on the right side. Only for some (RH included) the
right side is the left side :cool:

Bye, Jojo
 
J

James Kuyper

Jack said:
Chapter and verse, please. Where in ANY version of the C standard
does it define malloc() behaving the same as some C++ operator that
does not exist in C?

The key question is not whether the C standard defines them as being the
same (which it could not), or even whether the C++ standard defines them
as being the same (which it could, but does not). The only thing that is
needed to justify Mark's comment is that the C++ standard say
essentially the same thing about memory deallocated using the delete
operator that the C standard says about the memory deallocated by the
free() function. This is, in fact, the case. In both cases, it's
undefined behavior to even access the value of any pointer whose value
used to point into the block of memory being deallocated, much less to
dereference that value.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top