473,698 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does malloc() reuse addresses?

Hi all,

my question is:

if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc(). I would like to have confirmation on
whether this is practically a concern when pointers are used to
uniquely identify data structure instances - like in this example:

int isInstanceValid (myStrict* inst)
{
int i;
for (i=0; i<instCount; ++i)
if (instances[i] == inst)
return 1;

return 0;
}

In this example, if an instance is freed, and a pointer to it becomes
non-valid, and later a new structure is allocated in the list, the
function will return that the pointer is valid, although it is actually
not the instance that was originally referred.

Jul 14 '06 #1
48 5825
"avasilev" <al********@gma il.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
Hi all,

my question is:

if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc().
Yes. It happens all the time.
Jul 14 '06 #2
"avasilev" <al********@gma il.comwrites:
if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc().
Yes.
I would like to have confirmation on whether this is
practically a concern when pointers are used to uniquely
identify data structure instances - like in this example:
It's an incorrect approach. Strictly speaking the behavior of
doing anything with a pointer to freed memory yields undefined
behavior.

You're better off using a counter to stamp each new structure
with a unique serial number and then comparing those unique
serial numbers.
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
Jul 14 '06 #3

avasilev wrote:
Hi all,

my question is:

if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc().
It might; you cannot depend on it though.
I would like to have confirmation on
whether this is practically a concern when pointers are used to
uniquely identify data structure instances - like in this example:

int isInstanceValid (myStrict* inst)
{
int i;
for (i=0; i<instCount; ++i)
if (instances[i] == inst)
return 1;

return 0;
}

In this example, if an instance is freed, and a pointer to it becomes
non-valid, and later a new structure is allocated in the list, the
function will return that the pointer is valid, although it is actually
not the instance that was originally referred.
yes

goose,

Jul 14 '06 #4


shouldnt be a problem, because if you're really keeping track of valid
pointers with that table, anytime you do a free() on a pointer you MUST
be removing that entry from the table.

Jul 14 '06 #5
On 14 Jul 2006 11:48:15 -0700, "avasilev" <al********@gma il.com>
wrote:
>Hi all,

my question is:

if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc(). I would like to have confirmation on
whether this is practically a concern when pointers are used to
All in all a terrible plan.

Once an area of memory is freed, the pointer that was passed to free
becomes indeterminate so any attempt to evaluate that pointer invokes
undefined behavior.

Attempting to use the coincidence is extremely non-portable. Even if
the addresses should match up, how do you know that someone else did
not use (and free) the area in between the time you freed and
allocated it again. In a virtual memory system, even if the address
is the same it may be in a physically different portion of memory.
Some OSes reinitialize allocated memory (not necessarily to zero) as a
security precaution before allowing me to use it.
Remove del for email
Jul 14 '06 #6

avasilev wrote:
Hi all,

my question is:

if i allocate some memory with malloc() and later free it (using
free()), is there a possibility that a consequent malloc() will
allocate memort at the same starting address and will return the same
pointer as the previous malloc(). I would like to have confirmation on
whether this is practically a concern when pointers are used to
uniquely identify data structure instances - like in this example:

int isInstanceValid (myStrict* inst)
{
int i;
for (i=0; i<instCount; ++i)
if (instances[i] == inst)
return 1;

return 0;
}

In this example, if an instance is freed, and a pointer to it becomes
non-valid, and later a new structure is allocated in the list, the
function will return that the pointer is valid, although it is actually
not the instance that was originally referred.
If you are doing what you say with the instance array, why are you not
removing the pointer from the array when you free the element of the
list. Sounds like you might want to rethink your design a little.

Jul 14 '06 #7
"avasilev" <al********@gma il.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...

What (exactly) are you trying to accomplish? Maybe if we knew what it was,
we could offer a suitable and portable method of accomplishing it.
Jul 14 '06 #8

Ancient_Hacker wrote:
shouldnt be a problem, because if you're really keeping track of valid
pointers with that table, anytime you do a free() on a pointer you MUST
be removing that entry from the table.
Yes, but later I can allocate a new pointer and add it to the table, it
could happen to have the same value. Then a previously non-vaid pointer
becomes valid now.

Jul 14 '06 #9
"avasilev" <al********@gma il.comwrote in message
news:11******** *************@m 79g2000cwm.goog legroups.com...
>
Ancient_Hacker wrote:
>shouldnt be a problem, because if you're really keeping track of valid
pointers with that table, anytime you do a free() on a pointer you MUST
be removing that entry from the table.

Yes, but later I can allocate a new pointer and add it to the table, it
could happen to have the same value. Then a previously non-vaid pointer
becomes valid now.
But it does not necessarily even point to the same kind of object.

Some other malloc(), even inside a library call or DLL or some such thing,
may have allocated memory at that starting address. And if it has not been
allocated even testing to see what the value is invokes undefined behavior.

We can say for sure:

Broken plan, don't do it.

Really, really. Don't.
Jul 14 '06 #10

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

Similar topics

33
2711
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it too, up until I started posting here! Thanks, Chris
24
3806
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
4
1819
by: Manu | last post by:
Hello, Can we say that the return addresses from the various malloc function calls, in a program, will always be in a predefined order (increasing or decreasing, depeding on how the heap is managed) ? regards Manu
41
3329
by: jacob navia | last post by:
In the C tutorial for lcc-win32, I have a small chapter about a debugging implementation of malloc. Here is the code, and the explanations that go with it. I would appreciate your feedback both about the code and the associated explanations. ---------------------------------------------------------------------
6
3366
by: itsolution | last post by:
Hi folks, Could you shed some light on this issue? my program is running on Freebsd as a daemon. When user sends a request, it forks itself and lets its child process handles the request. And its main role is just to read a big xml file and save each object into its embedded DB(such as gdbm).
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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
7738
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.