Tuesday, 24 July 2012

Memory Layout in C



Types of memory

Memory layout in C
Whenever program is compiled five types of memories are given to the program to carry out its function
·         Text Segment
·         Initialized data segment
·         Uninitialized data segment
·         Stack
·         Heap

Text Segment-
Also known as the code segment
Contains executable instructions
Text segment is placed below stack and heap to prevent overflow
It is a read only memory so that it cannot be changed by accident

Initialize Data Segment-
Usually called Data Segment
It contains a static variables and global variables that are initialized by the program
It is a not a read only memory since it can be changed
Uninitialized Data segment-
Also known an bss segment (blog started by symbol)
It contains static and global variables which are initialized to zero or which are not explicitly initialized
Stack-
It contains the local or automatic variables
Even every memory required by the recursive functions also some from the stack memory.
Heap-
Heap memory is the segment where Dynamic memory allocation takes place
Heap area is shared by all the libraires and dynamically loaded modules in the process.
It has a disadvantage that it need to be manually freed thus leads to memory leaks or fault segment
Moreover variables allocated in stack memory are much faster to access than heap memory

Wednesday, 18 July 2012

MEMORY VIOLATIONS



MEMORY VIOLATIONS


Dangling Pointers and wild pointers are among  the few causes of memory safety violations.C and C++ provides with one of the most powerful tool programming world ever came with and this was Pointers but this gave programmer immense control over memory addresses and sometimes we try to acces memory which we are not suppose to access and this is called FAULT SEGMENT

Above we mentioned two types of memory violations-Dangling and Wild.
Dangling Pointers arises when some object is deleted or relocated or when some memory is made free without making pointer variable null. This happens  because even after deletion pointer points to the same location in memory even though the reference has been deleted or may be getting used for some other purposes

For example
void func()
{
    char *dp=malloc(1000)
free(dp); //here dp becomes dangling pointer
dp=NULL// now pd is null pointer and no longer a dangling pointer
}

Interesting thing about dangling pointers
char ch='a';
char *c=NULL;
c=&ch;
free(c);
printf("%c",*c);
It will still print the a because it still pointing to same memory location


Wild pointers-:
Main cause of it is not initializing the pointer variable to null when it is initially declared thus every pointer which has not been explicitly initializes is considered as a Wild pointers
For example

int f(int i)
{
char *dp//wild pointers
static char *scp//not a wild pointer because static initializes it to 0
}


SMART POINTERS
its a type of abstract data type and provides the additional features to the pointers such as automatic garbage collection and bound checking and is very useful in removing Dangling pointers.They set the pointers automatically to null when ther object is deleted. Java and C# dosent requires any smart pointers because they have there own garbage collection techniques
LISP was the first language having garbage collection