473,516 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory

Dear All,

Is it true that we can use dynamically-allocated memory after we free
it. ?
And if it is possible then how to make it sure that it can not be
used once we have freed
the dynamically allocated memory in our program.

Regards
Ranjeet

Nov 21 '05 #1
10 1735
On 20 Nov 2005 23:02:28 -0800, ra***********@gmail.com wrote in
comp.lang.c:
Dear All,

Is it true that we can use dynamically-allocated memory after we free
it. ?
No, it is not. Not if "we" want defined behavior.
And if it is possible then how to make it sure that it can not be
used once we have freed
the dynamically allocated memory in our program.


One way is use proper programming methods. Another, that works in
some cases, is to set pointers to NULL after freeing them, and always
checking them before using. But that doesn't work if there might be
copies of the original pointer around.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 21 '05 #2
ra***********@gmail.com <ra***********@gmail.com> a écrit*:
Is it true that we can use dynamically-allocated memory after we free
it. ?
When you free a dynamically allocated memory, you tell to the
virtual machine 'I will no more use it, you can reuse it'.
But often, the virtual machine do not take it again ASAP.
So, often, you can free memory a reuse it. But sometime,
the virtual machine take it again as soon as you free it.

Did you expect you code to run successul often, sometime,
when the sun shines ?
And if it is possible then how to make it sure that it can not be
used once we have freed
the dynamically allocated memory in our program.


What is your problem ? Could you explain why you want to
do this ?

Marc Boyer
Nov 21 '05 #3
ra***********@gmail.com wrote:
Dear All,

Is it true that we can use dynamically-allocated memory after we free
it. ?
Only if you don't mind your program breaking in some unpredictable
way.

If `p` points to mallocated memory, after `free(p)` you are not allowed,
on pain of Undefined Behaviour, to do anything with the value of `p` or
any other pointer that pointed into the memory `p` pointed at.

The memory may no longer exist: it is permitted for the implementation
to return the address to the operating system, which might then unmap
the relevant piece of virtual address space and make the pointer value
invalid.

Even if it /does/ exist, its content may have been changed in arbitrary
ways: it may have been trampled on by housekeeping pointers, it may have
been cleared, it may have had a standard bitpattern sprayed onto it,
eg 0xdeadbeef, to make misuses easier to detect.
And if it is possible then how to make it sure that it can not be
used once we have freed
the dynamically allocated memory in our program.


Discipline, on the lines of "don't DO that".

--
Chris "another virtual machine" Dollin
missing tests don't fail.
Nov 21 '05 #4
int *ptr = (int*)malloc(sizeof(int)); /* allocating memory: chunk a */
free(ptr); /* chunk a is freed, but _pointer_ ptr is still alive. */
ptr = NULL; /* done with it, no one can access that again, */

ra***********@gmail.com wrote:
Dear All,

Is it true that we can use dynamically-allocated memory after we free
it. ?
And if it is possible then how to make it sure that it can not be
used once we have freed
the dynamically allocated memory in our program.

Regards
Ranjeet

Nov 21 '05 #5
ra***********@gmail.com said:
Dear All,

Is it true that we can use dynamically-allocated memory after we free
it. ?
Is it true that we can jump off a cliff whilst attached to a strong elastic
rope, get back on the cliff, take off the rope, and jump off the cliff
again?
And if it is possible then how to make it sure that it can not be
used once we have freed
the dynamically allocated memory in our program.


free(p);
p = NULL;

is a good start, but doesn't beat aliasing.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 21 '05 #6
Blair Craft wrote:
int *ptr = (int*)malloc(sizeof(int)); /* allocating memory: chunk a */
Why cast?
free(ptr); /* chunk a is freed, but _pointer_ ptr is still alive. */
ptr = NULL; /* done with it, no one can access that again, */


In real code, between the malloc and the free the pointer may be copied
into other places. Nulling the pointer variable one has free'd doesn't
fix them, so thinking that it helps is ... iffy.

--
Chris "another virtual machine" Dollin
missing tests don't fail.
Nov 21 '05 #7
Maybe the OP is worried about others reading the free'ed memory and is
looking for a 100% guaranteed method that any data in mem has gone.

Come on OP where have you gone?

Nov 21 '05 #8
ra***********@gmail.com wrote
(in article
<11*********************@g14g2000cwa.googlegroups. com>):
Dear All,

Is it true that we can use dynamically-allocated memory after we free
it. ?
Not at all. It is true that a lot of people /try/ to do this,
and generally suffer the consequences. One very unfortunate
aspect of undefined behavior in C programs is that sometimes
code that is demonstrably broken will accidentally work anyway,
and the result of this seemingly unlikely result is to convince
programmers that they can get away with it. The truth is that
they just got very UNlucky and may not realize it until much
later.
And if it is possible then how to make it sure that it can not be
used once we have freed
the dynamically allocated memory in our program.


It's all but impossible to guarantee it won't happen through
some crutch method or language feature. however, you can code
to avoid it or perhaps eliminate it, if you are careful. There
is no foolproof method that I am aware of other than programmer
skill. Some folks always check for NULL before dereferencing a
pointer, and nevermind the performance consequences (I am one of
those). Some folks replace free() with a version to sets the
pointer to NULL afterward. That works in some, but not all
cases.

In any event, if someone told you it was okay to use it after
the fact, you owe it to the universe to convince them otherwise
before they spread such code around further. If you read it in
a book, please let us know the source.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 21 '05 #9
carl wrote:

Please provide context. Search this group for "google context" for
instructions on how to do this using Google.
Maybe the OP is worried about others reading the free'ed memory and is
looking for a 100% guaranteed method that any data in mem has gone.
If so, then the answer is highly platform specific and it may not be
possible.
Come on OP where have you gone?


No one knows.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 21 '05 #10
Blair Craft <yu****@postwebpro.com> writes:
int *ptr = (int*)malloc(sizeof(int)); /* allocating memory: chunk a */
free(ptr); /* chunk a is freed, but _pointer_ ptr is still alive. */
ptr = NULL; /* done with it, no one can access that again, */


or better:

#include <stdlib.h>

void foo(void)
{
int* ptr = malloc(sizeof *ptr);
/* do stuff with ptr */

free(ptr);
ptr = NULL; /* Not much harm, but doesn't really help much either */

/* do other stuff, otherwise the assignment above is totally pointless */
}

No need to cast void*. Some people here say don't the return value of
malloc, since it can hide the fact that stdlib.h isn't included. I have
another philosophy which is: don't cast anything at all if it isn't
really necessary... And if it is really necessary, you should first think
if there isn't an alternative way to accomplish the goal. The type casts
should only be used as a last resort.

About assigning NULL to freed pointers... It really doesn't help that
much, for two reasons:

* In my experience free statements are either often very near the end
of a function and the pointer varible will go out of scope soon enough
anyway.

* When memory from the free store (malloc and friends) there are usually
more than one pointer to the memory in question, and assigning NULL to
just one pointer won't affect all the other pointers, and it is these
more complicated situations that leads to buggy code anyway.

What really does help is a careful design where lifetime and ownership
of objects is specified, together with coding discipline.

/Niklas Norrthon
Nov 23 '05 #11

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

Similar topics

0
2030
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
4
12997
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage reported through task manager peaks between 41-42MB. I've stopped and restarted the MSSQLserver service and checked that the running values are...
4
2565
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
9
2329
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When...
22
3449
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from application server, will each connection take the memory 800M (200000 x 4k = 800 M), so the memory took will be 800M times number of connections, or the...
14
20748
by: Alessandro Monopoli | last post by:
Hi all, I'm searching a PORTABLE way to get the available and total physical memory. Something like "getTotalMemory" and it returns the memory installed on my PC in bytes, and "getAvailableMemory" and it returns the available memory in bytes. Do you know is there's a C function, a c++ Object or anything else that compiles in Linux and...
1
3003
by: Nick Craig-Wood | last post by:
I've been dumping a database in a python code format (for use with Python on S60 mobile phone actually) and I've noticed that it uses absolutely tons of memory as compared to how much the data structure actually needs once it is loaded in memory. The programs below create a file (z.py) with a data structure in which looks like this --...
5
24606
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day...
1
2021
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after the objects for which that memory was allocated are no longer live in the process. This is only a leak if peak memory goes up again each time you...
5
505
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store pointers of type structure "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_STRUCT" using...
0
7276
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...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7142
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...
0
7548
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...
0
4773
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...
0
3267
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...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
488
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...

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.