开发者

Program terminating abnormally if input is very large

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

#define n ((sizeof(char)) * 100 )

int stringlength(char * str)
{
    int count=0;
    while(*str)
    {  
        if(*str == '\n')
        {
            *str=0;
        }
        else
            count++, str++;
    }
    return count;
}


int palin1(char *str, int k)
{
    char * pend = str + k - 1;
    if(*pend != *str)
        return 0;
    else
        palin1(str+1, k-1);
       return 1;
}    

int palin(char *str)
{
    int length = stringlength(str), f=0;
    char *pend = str + length - 1;
    while(str <= pend)
    {
        if(*str == *pend) f=1;
        else
            return (f = 0);
        str++, pend--;
    }
    return 1;
}

main()
{
    char * ps = (char *)malloc(n);
    int flag;
    if(ps == NULL) printf("Malloc Fail\n");
    else
    {
        printf("Malloc Succeeded, you have memory of %d bytes\n", n);
        printf("This program checks if String is Palindrome or not\n\
        \nEnter your String: ");
        fgets(ps, 100, stdin);
        printf("You entered: %s of length %d", ps, stringlength(ps));
        int i = 0;
        printf("\n\nEnter:\n1.Using iteration\n2.Using Recursion ");
        scanf("%d", &i);
        switch(i)
        {
            case 1:
                flag=palin(ps);
                break;
            case 2:
                flag=palin1(ps,stringlength(ps));
                break;
            default:
                printf("Invalid input");
        }

        if(flag) printf("\nYou entered a Palindrome");
        else  printf("\nNot a Palindrome");
    }
    free (ps);
    return 0;
}

Why does the above program http://www.ideone.com/qpGxi does not give any output on putting the input:

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

I know fgets(ps,100,stdin) will take only 100 characters and not more than开发者_JAVA技巧 that, but why does the program halt execution?


You should check for fgets failure, as recommended by the fgets spec.

if ( fgets(ps,100,stdin) == NULL ) {
    printf("Input failed.");
    //check for 'feof' or 'ferror' here
    return -1;
}
printf("You entered: %s of length %d",ps,stringlength(ps));

I don't see why fgets would be failing, but you would get an uninitialized character buffer back, which would crash printf.

EDIT: You should really pay attention to your compiler warnings, too.

prog.c:49: warning: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:59: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result
prog.c:63: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c: In function ‘palin’:
prog.c:46: warning: control reaches end of non-void function
prog.c: In function ‘main’:
prog.c:52: warning: ‘flag’ may be used uninitialized in this function

You can see that even your compiler recommends checking fgets for null. Also, flag should be set to 0 in the default case, otherwise you will get undefined behavior if the user enters something other than 1 or 2.

EDIT 2: Oh for Christ's sake! your program works fine! You forgot to check "run program" in Ideone!!!

http://www.ideone.com/7ecZd


You cannot break a string literal just like that

printf("%s\n", "string literal **WRONGLY**\n
broken right after the line break.");

What you can do is use the preprocessor feature of joining successive string literals to make just one

printf("%s\n", "string literal **CORRECTLY**\n"
"broken because the preprocessor joins these 2 parts.");


It's terminating because there are characters left in the input stream if the input is too large. For example, if you wish to take only 5 characters using fgets but have given the input as -

StackOverflow

Overflow are left in the input stream. They need to be removed from the stream for further input operations to succeed. So, remove those extra characters from the stream using -

fgets(ps,100,stdin);
while (getchar() != '\n');

Since the input stream is struck with offending characters, the scanf statement that actually takes the user input is not working and jumping to subsequent operations.

Also initialize the flag variable to 0 other wise it has garbage values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜