473,721 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

use delete to destroy primitive/object types but memory is not freed

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(doubl e)<<" "<<sizeof(*g)<< " "<<*g<<" "<<endl;
*g = 111;
cout<< sizeof(g)<<" "<<sizeof(doubl e)<<" "<<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,
jimjim
Nov 14 '05 #1
30 3730
jimjim <Fr*********@bl ueyonder.co.uk> spoke thus:
cout<< sizeof(g)<<" "<<sizeof(doubl e)<<" "<<sizeof(*g)<< " "<<*g<<" "<<endl;
cout<< sizeof(g)<<" "<<sizeof(doubl e)<<" "<<sizeof(*g)<< " "<<*g<<" "<<endl; 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!


(comp.lang.c++ is that way ----->)

Your post is off-topic for comp.lang.c. Please visit

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

for posting guidelines and frequently asked questions. Thank you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
jimjim <Fr*********@bl ueyonder.co.uk> spoke thus:
cout<< sizeof(g)<<" "<<sizeof(doubl e)<<" "<<sizeof(*g)<< " "<<*g<<" "<<endl;
cout<< sizeof(g)<<" "<<sizeof(doubl e)<<" "<<sizeof(*g)<< " "<<*g<<" "<<endl; 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!


(comp.lang.c++ is that way ----->)

Your post is off-topic for comp.lang.c. Please visit

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

for posting guidelines and frequently asked questions. Thank you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
jimjim wrote:

Hello,

This is a simple question for you all, I guess .
int main(){
double *g= new double;
[...]


Stop right there: The question just becaue non-simple
for us-all here in comp.lang.c. Perhaps it would be simple
for them-all over thar in comp.lang.c++. Tread carefully
as you cross the demilitarized zone.

--
Er*********@sun .com
Nov 14 '05 #4
jimjim wrote:

Hello,

This is a simple question for you all, I guess .
int main(){
double *g= new double;
[...]


Stop right there: The question just becaue non-simple
for us-all here in comp.lang.c. Perhaps it would be simple
for them-all over thar in comp.lang.c++. Tread carefully
as you cross the demilitarized zone.

--
Er*********@sun .com
Nov 14 '05 #5
I knew it was an easy question. I should have looked at a faq first.

The answer is at http://www.eskimo.com/~scs/C-faq/q7.21.html as Christopher
pointed out.

Thanks for the reply :-)

Nov 14 '05 #6

change new to malloc and delete to free and if you dont know the answer
visit http://www.eskimo.com/~scs/C-faq/q7.21.html as Christopher pointed
out.

Nov 14 '05 #7
This layout http://www.lysator.liu.se/c/c-faq/c-faq-toc.html of the faq in
the previous post is easier for us the newbies in C to locate the
questions/answers ;-)

Pay attention at the top of the page. There are link to faqs that are also
extremely interesting for newbies like us to read. Most of my questions are
there!! ftp://rtfm.mit.edu/pub/usenet/news.answers/C-faq/faq as well as this
pub/usenet/comp.lang.c/ and this ftp.uu.net (directory
usenet/news.answers/C-faq/ )
Nov 14 '05 #8
On Wed, 07 Apr 2004 21:11:52 GMT, "jimjim"
<Fr*********@bl ueyonder.co.uk> wrote:

change new to malloc and delete to free and if you dont know the answer
visit http://www.eskimo.com/~scs/C-faq/q7.21.html as Christopher pointed
out.

I'll tell you a secret: Eric does know the answer. When you're
off-topic, you're off-topic. Don't argue about it, just take the good
advice you are given.

Even if the simple substitution you propose actually worked (it
doesn't) there are still differences between C and C++ memory
allocation, and comp.lang.c++ is where you'll get the right answers,
reviewed by the right people.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #9
jimjim wrote:

This is a simple question for you all, I guess .

int main(){
double *g = new double;
double* g = (double*)malloc (sizeof(double) );
*g = 9;
delete g;
free((void*)g);
cout<< sizeof(g)<<" "<<sizeof(doubl e)<<" "<<sizeof(*g)<< " "<<*g<<" "<<endl;
fprinf(stdout, ...
*g = 111;
cout<< sizeof(g)<<" "<<sizeof(doubl e)<<" "<<sizeof(*g)<< " "<<*g<<" "<<endl;
fprintf(stdout, ...
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 don't 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!
delete g;

or

free((void*)g);

delete's or free's the object to which g points.
g is still a valid [pointer] object
but the object to which it points is *not*.
Any reference to *g is *undefined* --
not defined by either the C or C++ standards
after *g is delete'd or free'd.
cat main.cc #include <iostream>

int main(int argc, char* argv[]) {
double* p = new double;
*p = 9;
delete p;
double* g = new double;
std::cout << sizeof(g) << ' ' << sizeof(double) << ' '
<< sizeof(*g) << ' ' << *g << std::endl;
*p = 111;
std::cout << sizeof(g) << ' ' << sizeof(double) << ' '
<< sizeof(*g) << ' ' << *g << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

4 8 8 9
4 8 8 111

Notice that, after *p is delete'd (free'd),
the storage be allocated for another object (*g in this case).

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9365
by: Eric | last post by:
Hi, I'm used to C/C++ where if you "new" something you really need to "delete" it later. Is that also true in javascript? if i do "mydate = new date();" in a function and dont "delete mydate" when the function exits do i have a memory leak or other trouble brewing? ie: function MyFn() { var mydate;
28
490
by: jimjim | last post by:
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;
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9131
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.