Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do memory addresses on the heap get reused?

Tags:

c++

memory

A further explanation of the title question is in order, let me explain my scenario.

I have a list container of pointers to several objects on the heap. Whenever a new object is created a pointer to it is added to the list and whenever an object is deleted its pointer is removed. It is safe to say that all pointers on this list are always valid.

Many of the objects on the list contain pointers to other objects on the same list.

Before I dereference any of those pointers I would like to use a CheckAgainstList(ptr*) function to make sure that the one object is pointing to another object on the same list and is therefore not pointing to an object that has since been deleted.

Get your tinfoil hats on now, Is this possible?

  1. Object A has a pointer to object B with a memory address of 0x00988e50.
  2. Object B is deleted.
  3. Object C is created and is placed into the newly freed memory space 0x00988e50.
  4. CheckAgainstList(ptr*) returns true when we check the pointer because object C is on the list and is in the same memory address B used to occupy.

Now we have a bug because A thinks it has a pointer to B, but B is gone and C has taken its place so to speak.

Is this potential bug even possible?

like image 512
user1438585 Avatar asked Jun 22 '12 17:06

user1438585


People also ask

Why is heap memory allocation not safe?

Heap memory allocation isn’t as safe as Stack memory allocation was because the data stored in this space is accessible or visible to all threads. If a programmer does not handle this memory well, a memory leak can happen in the program.

What is the heap in embedded systems?

The heap is where the dynamic memory of the system is located. Dynamic memory and the heap can in many cases be considered optional in small embedded systems. Dynamic memory makes memory sharing possible between different pieces of a program.

Why is it called heap memory?

It is called heap because it is a pile of memory space available to programmers to allocated and de-allocate. Every time when we made an object it always creates in Heap-space and the referencing information to these objects are always stored in Stack-memory.

What are the stack and the heap?

The stack and the heap are fundamental to an embedded system. Setting up the stack and the heap properly is essential to system stability and reliability. Incorrectly used, they may cause your system to wreak havoc in the strangest ways. Stack and heap memory must be allocated statically by the programmer.


2 Answers

Not only is it possible, it's very likely. A good memory allocator will try to reuse memory as often as possible to reduce fragmentation and bloat.

The problem you're trying to solve might be amenable to a weak_ptr, which can be checked for validity before it's used.

like image 109
Mark Ransom Avatar answered Oct 14 '22 09:10

Mark Ransom


Yes that bug is entirely possible.

Basically what you are doing is pretty dangerous and will lead to bugs pretty quickly. You may be best off using a reference counted smart ptr of some sort. C++11 includes std::shared_ptr which means you can use in place of your normal pointer. This way the memory won't get freed until everything has finished with it and would alleviate issues like you describe.

Your only other option would be to scan through all other objects to see if they reference the deleted 'B' and do something like "null" out their pointers to the, now deleted, pointer.

like image 21
Goz Avatar answered Oct 14 '22 08:10

Goz