Saturday, 12 October 2013

Sanfoundry (Interview Question) INTERNSHIP

/*
 * Sanfoundry.com Memory Leak Program – India Internship Bangalore
 * Author: Manish Kumar Bhojasia, Location: Bangalore, India
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define KB1     1024
#define MEMSIZE (4 * KB1)

char *string = "IDENTIFY 10 ISSUES IN THIS MEMLEAK PROGRAM";
char *gstring = "IDENTIFY 10 ISSUES IN THIS MEMLEAK PROGRAM";
char *gptr;
int num;

char * f1_alloc(void);
char * f2_copyme(void);
void   f3_free(char *);

main()
{
    srand((int)getpid());
    while (1) {
        gptr = f1_alloc();
        strcpy(gptr, string);
        f2_copyme();
        f3_free(gptr);
    }
}

char *f1_alloc()
{
    return (malloc(MEMSIZE));
}
char *f2_copyme()
{
    static int len, i = 0;
    char *s;

    len = strlen(string);
    s = (char *)malloc(len);
    strncpy(s, string, len);
    i++;
    if (!(i % MEMSIZE))
        printf("i=%d, %s\n", i, s);
}

void f3_free(char *p)
{
    num = rand() % 3;
    if (!num)
        free(p);
}


This program will hang your computer or forcibly terminated.
Error’s in this program are::
1.       LINE No 11-> the program has used macro’s to define MEMSIZE (4*KB) which is a very large value and in whole program it will not be utilised and it just a wastage of memory.
2.       LINE No 13-> char * string is initialized with a string but this is not a good practice to store a address of a string in a pointer without declaring the size of the pointer and it can cause overwriting of memory.
3.       LINE No 14-> char * string is initialized with a string but this is not a good practice to store a address of a string in a pointer without declaring the size of the pointer and it can cause overwriting of memory.
4.       LINE No15-> will show a logical error as loop is declared for infinite times.
5.       LINE No35->Memory allocating many numbers of times but de allocation is very rarely.
6.        LINE No 39-> “len” is declared as static and its value is overwrite again and again which is not required when we had declared it as static.
7.       LINE No43-> in this “s” is allocated with memory every time the function is called but de allocation is not there.
8.       “MEMSIZE” is replaced by a huge memory “4 * KB” but most of the memory is wasted.
9.       Program generally shows “segmentation error” because de allocation and allocation are not parallel.

10.   LINE 37-> In function “f2_copyme() has a return type but function does not return any thing which can also be considered as an error. http://www.sanfoundry.com/internship/

Thursday, 19 September 2013

Program for converting infix string to postfix using Stack implementation

Aim: Infix to Postfix
Steps:
1. Input the infix string
2. The program use the stack implementation to convert this infix to postfix
3. There are 5 function are used
     Pop() is used to pop the elements
     Push() is used to push the element in the stack
     createstack() is used to create head node
     check() is used to test whether the element has to push or print
     priority() is used to check the priority of the operators


output







Program


#include<stdio.h>
int priority(char a);
int check(char a);
typedef struct node
{
        void *data;
        struct node *link;
}stacknode;
typedef struct
{
        int count;
        stacknode *top;
}stack;
stack *createstack()
{
      stack *sp;
      sp=(stack*)malloc(sizeof(stack));
      sp->top=NULL;
      sp->count=0;
}
void push(stack *sp,void *data)
{
     stacknode *node;
     node=(stacknode*)malloc(sizeof(stacknode));
     node->data=data;
     node->link=sp->top;
     sp->top=node;
     sp->count++;
}
void *pop(stack *sp)
{
     stacknode *temp;
     void *data;
     data=sp->top->data;
     temp=sp->top;
     sp->top=sp->top->link;
     free(temp);
     sp->count--;
     return(data);
}
void *top(stack *sp)
{
       return(sp->top->data);    
}
int main()
{
    stack *sp;
    char s[100];
    int i;
    int j;
    char *a;
    sp=createstack();
    printf("\nenter the string\n");
    scanf("%s",s);
    for(i=0;i<=strlen(s);i++)
    {
       if(check(s[i]))
       {
          printf("%c",s[i]);
       }
       else if(s[i]==')')
       {
            while(*(char*)top(sp)!='(')
            {
                a=(char*)pop(sp);
                printf("%c",*a);
                free(a);
            }
            a=(char*)pop(sp);
            free(a);
       }
       else
       {
           if(s[i]=='(')
           {
               a=(char*)malloc(sizeof(char*));
               *a=s[i];
               push(sp,a);
           }
           else
           {
               if(s[i]=='('||priority(*(char*)top(sp))<priority(s[i]))
               {
                  a=(char*)malloc(sizeof(char*));
                  *a=s[i];
                  push(sp,a);
               }
               else
               {
                         a=(char*)pop(sp);
                         printf("%c",*a);
                         free(a);
                         while(priority(*(char*)top(sp))>=priority(s[i])&&*(char*)pop(sp)!='(')
                         {
                            a=(char*)pop(sp);
                            printf("%c",*a);
                            free(a);   
                         }
                         if(!check(s[i]))
                         {
                           a=(char*)malloc(sizeof(char*));
                           *a=s[i];
                           push(sp,a);
                         }          
               }
           }
       }
    }            
    free(s);
    free(sp);
    getch();
    return(0);
}
int priority(char a)
{
    switch(a)
    {
        case '(': return(-1);
        break;     
        case '/': return(1);
        break;
        case '*': return(1);
        break;
        case '-': return(0);
        break;
        case '+': return(0);
    }
    return(0);
}
int check(char a)
{
    if(a=='*'||a=='/'||a=='-'||a=='+'||a=='('||a==')')
    {
        return(0);
    }
    else    
    {
       return(1);
    }
}            








Tuesday, 17 September 2013

Program For Eight Queen Problem

Output



#include<stdio.h>
#include<malloc.h>
#include<conio.h>

// Declaration of Structure
typedef struct p
{
int row;
int col;
}position;

typedef struct node
{
position *p;
struct node *link;
}structnode;
typedef struct
{
int count;
structnode *link;
}stack;

//  Declaration of Function
int getsize();
int guarded(int board[9][9],int row,int col,int boardsize);
void *createstack();
void fillboard(stack *st,int boardsize);
void push(stack *st,position *p);
position pop(stack *st);
void printboard(stack *st,int boardsize);

// Main function
int main()
{
position p;
int boardsize;
stack *st;
boardsize=getsize();
st=(stack*)createstack();
fillboard(st,boardsize);
printboard(st,boardsize);
printf("\nwe hope you enjoyed queens\n");
getch();
}

// Return valid Size of the board
int getsize()
{
int boardsize;
printf("welcome toeight queens.you may select\n"
"a board size from 4*4 to 8*8.i will\n"
"then position a queen in each row of the\n"
"board so no queen may capture another\n"
"queen.note:there is no solution for board\n"
"less than 4*4 and more than 8*8\n");
printf("\nenter the size of board\n");
scanf("%d",&boardsize);
while(boardsize<4||boardsize>8)
{
printf("board size is greater than 3 and\n"
       "lesst han 9.you entered %d.\n"
       "please re-enter.thankyou.\a\a\n\n"
       "your board size:",boardsize);
scanf("%d",&boardsize);
}
return(boardsize);
}

// Create Stack header
void *createstack()
{
stack *st;
st=(stack*)malloc(sizeof(st));
st->count=0;
st->link=NULL;
return(st);
}

// Correct arrangements for queens on board
void fillboard(stack *st,int boardsize)
{
int row;
int col;
int board[9][9]={0};
position *p;
position s;
row=1;
col=0;
while(row<=boardsize)
{
while(col<=boardsize&&row<=boardsize)
{
col++;
if(guarded(board,row,col,boardsize)==0)
{
board[row][col]=1;
p=(position*)malloc(sizeof(position));
p->row=row;
p->col=col;
push(st,p);
row++;
col=0;
}
while(col>=boardsize)
{
s=pop(st);
row=s.row;
col=s.col;
board[row][col]=0;
}
}
}
}

// Stack push
void push(stack *st,position *p)
{
structnode *node;
(st->count)++;
node=(structnode*)malloc(sizeof(structnode));
node->p=p;
printf("\npush %d %d",node->p->row,node->p->col);
node->link=st->link;
st->link=node;
}
position pop(stack *st)
{
void *temp;
position s;
s.row=st->link->p->row;
s.col=st->link->p->col;
printf("\npop %d %d",s.row,s.col);
free(st->link->p);
temp=st->link;
st->link=st->link->link;
free(temp);
(st->count)--;
return(s);
}

// checking queen position
int guarded(int board[][9],int chkrow,int chkcol,int boardsize)
{
int row;
int col;
col=chkcol;
for(row=1;row<=chkrow;row++)
{
if(board[row][col]==1)
{
return(1);
}
}
for(row=chkrow-1,col=chkcol+1;row>0&&col<=boardsize;row--,col++)
{
if(board[row][col]==1)
{
return(1);
}
}
for(row=chkrow-1,col=chkcol-1;row>0&&col>0;row--,col--)
{
if(board[row][col]==1)
{
return(1);
}
}
return(0);
}

// Board
void printboard(stack *st,int boardsize)
{
int col;
position s;
stack *sp;
position *p;
sp=(stack*)createstack();
while(st->count!=0)
{
s=pop(st);
p=(position*)malloc(sizeof(position));
p->row=s.row;
p->col=s.col;
push(sp,p);
}
free(st);
if(sp->count==0)
{
printf("\nthere are no position on this board\n");
return;
}
printf("\nplace queens in following positions\n");
while(sp->count!=0)
{
s=pop(sp);
printf("row %d-col %d:\t|",
 s.row,s.col);
for(col=1;col<=boardsize;col++)
{
if(s.col==col)
{
printf(" Q|");
}
else
{
printf("  |");
}
}
printf("\n");
}
free(sp);


}