PL101 - Programming Languages
PL101 - Programming Languages
Programming
Languages
Objectives
Memory is used to store the program itself and the run-time system nee
ded to support it.
Manual memory management
C Language
Segmentation is the process in which the main memory of the computer is logically d
ivided into different segments and each segment has its own base address. It is basicall
y used to enhance the speed of execution of the computer system, so that the processor
is able to fetch and execute the data from the memory easily and fast.
• Allows the placing of code data and stack portions of the same program in different
parts i.e segments of the memory, for data and code protection.
• Permits a program and its data to be put into different areas of memory each time
program is executed, i.e, provision for relocation may be done.
What are the memory segments?
•text (code) segment
The text segment (often called code segment) is where the compiled
code of the program itself resides.
•stack segment
The stack is the section of memory that is allocated for automatic
variables within functions.
LIFO
•heap segment
An area of memory used for dynamic memory allocation.
What is dangling pointer?
Dangling Pointer
A pointer pointing to a memory location that has been deleted (or freed) is called dang
ling pointer. There are three different ways where Pointer acts as dangling pointer.
What is dangling pointer?
Deallocation of Memory
void main()
{
int *ptr;
.....
.....
{
int ch;
ptr = &ch;
}
.....
// Here ptr is dangling pointer
}
What is a space leak?
Space Leak
There is a space leak when memory is lost and cannot be reclaimed. Space cannot be lo
st for-ever without reaching the limit on the available memory. A long-running progra
m with a space leak will eventually crash.
Space Leak
Space leaks are often detected relatively late in the development process, an
d often only in response to user complaints of high memory usage.
If space leaks could be detected earlier say for example as soon as they were i
ntroduced then they would be easier to fix and would never reach end users
Space leaks remain a thorn in the side of lazy evaluation, producing a signifi
cant disadvantage. (Haskell)
What is Memory Leaks?
Memory Leak
The reality is that memory leaks can strike any application in any language.
They’re more common in older or “closer to the metal” languages like C or C
++.
#include <stdlib.h>
#include <unistd.h>
int main() {
while(1) {
malloc(1024); // memory leak!
sleep(0.5); // slow down the leak
}
return 0;
}
How can I detect a memory leak?
The simplest way to detect a memory leak is also the way you’re most likely to find on
e: running out of memory.
If you do, it can be time to start digging into your code to figure out just what’s using u
p all your RAM.
Profiling tool
A profiling tool isn’t a silver bullet: it won’t tell you right away which parts of you
r application are leaking memory. What it will do is tell you which parts of your applica
tion are using the most memory.
Garbage collectors
Another important way to prevent memory leaks is to write code which disposes of un
needed resources.
If you’ve found a memory leak in your code, don’t view it as a failure of software. Inste
ad, look at it as a chance to improve your craft
•Reclaims objects that are no longer being used, clears their memory, and keeps the m
emory available for future allocations. Managed objects automatically get clean conten
t to start with, so their constructors don't have to initialize every data field.
•Provides memory safety by making sure that an object cannot use the content of anot
her object.
Limitations of Garbage Collectors
Memory can still leak
In automatic reference counting systems, such as Perl or Objective-C, memory is
leaked whenever there are cyclical references, since the reference count is never
decremented to zero.
A stranger type of memory leak can occur if the garbage collector is conservative. A
conservative garbage collector assumes that any memory that looks like a valid
address to an allocated object is a pointer to that object.
Identify the memory management and the problems encountered with the
assigned programming language.