Question

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) (5 marks) 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) (5 marks 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 incre- mentally 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I will answer part b first then i will move to part a.

b). Advantage:

An advantage of this scheme is that it can run in small chunks of time closely interwoven with the execution of the program. This characteristic makes it particularly suitable for real-time environments where the program can't be interrupted for very long.

Disadvantage:

1. Refcounting GC adds significant code/perf bloat as for every assignment calls to update refCount has to be added. Moreover, for multi-threaded system it can prove to be a major issue as locks have to be taken when updating refCount. Even the most performant atomic operations (e.g. Win32 InterlockedIncrement) are costly when used repeatedly.

2. Another disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another, for example, a parent object that has a reference to its child object, which has a reference back to its parent. These objects will never have a reference count of zero even though they may be unreachable by the roots of the executing program.

3. Another disadvantage is the overhead of incrementing and decrementing the reference count each time.

a.)Dangling Pointer

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Initially

ptr 5000 25 8000 5000

Later

ptr 5000 25 8000 5000

Example

#include<stdio.h>

int *call();
int main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);
return 0;
}
int * call(){

int x=25;
++x;

return &x;
}

It's output will be garbage because variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.

Add a comment
Know the answer?
Add Answer to:
Programs written in a language that supports pointers and dynamic memory allocation can suffer from dangling...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT