Give a scenario that can cause lost heap-dynamic variables


Programs written in a language that supports pointers and dynamic memory allocation can suffer from dangling pointers and lost heap-dynamic variables (or garbages).

(a) A dangling pointer is a pointer that contains the address of a piece of memory that no longer belongs to the program. Give one scenario that can cause dangling pointers.

Support your scenario with sample codes in your favorite language. Poorly implemented languages leave the detection of the dangling pointer problem to the operating system. Suggest a language implementation method to allow run-time detection of dangling pointers by the language.

(b) A lost heap-dynamic variable (or garbage) is an allocated heap-dynamic variable that is no longer accessible to the user program, although it still belongs to the program. Give a scenario that can cause lost heap-dynamic variables. Support your scenario with sample codes in your favorite language.

The reference counter method is a way to detect garbages and reclaim the storage incrementally as soon as garbages are created. It works by maintaining in every cell a counter that stores the number of pointers that are currently pointing at the cell. Embedded in the decrement operation for the reference counters, which occurs when a pointer is disconnected from the cell, is a check for a zero value.

If the reference counter reaches zero, it means that no program pointers are pointing at the cell, and the cell has thus become garbage and can be returned to the list of available space. Give three distinct disadvantages/problems with the reference counter method.

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Give a scenario that can cause lost heap-dynamic variables
Reference No:- TGS02902657

Expected delivery within 24 Hours