473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1732
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
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...
4
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...
4
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...
9
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...
22
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...
14
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...
1
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...
5
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...
1
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...
5
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.