user3606997
user3606997

Reputation: 111

Why this shows runtime error?

I was coding to create a my own scanf type function, when this problem occurred. The below code works perfectly when it is tested in the console.

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

inline void fastint(int &x){
    register int c = getchar();
    x = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar());
        for(; c>47 && c<58 ; c = getchar()){
            x = (x<<1) + (x<<3) + c - 48;
        }
}

inline void faststring(char * arr){
    register char c = getchar();
    register int i = 0;
    while (c < 33)
        c = getchar();
    while (c != '\n'){
        arr[i] = c;
        c = getchar();
        i = i + 1;

    }
        arr[i] = '\0';
}

int main(){
    int i;
    char * arr = (char*)malloc(20*sizeof(char));
    fastint(i);
    printf("number is %d\n",i);
//  fprintf(stderr, "new format %d\n", that_pesky_x);
        fastint(i);
    printf("number is %d\n",i);
        fastint(i);
    printf("number is %d\n",i);
        fastint(i);
    printf("number is %d\n",i);
        fastint(i);
    printf("number is %d\n",i);

    faststring(arr);
    printf("string is %c\n",arr[3]);

        faststring(arr);
    printf("string is %c\n",arr[3]);

        faststring(arr);
    printf("string is %c\n",arr[3]);
        faststring(arr);
    printf("string is %s\n",arr);
        faststring(arr);
    printf("string is %s\n",arr);
        faststring(arr);
    printf("string is %s\n",arr);

    return 0;
}

But when the above code is tested on a text file(named input.txt) whose contents are:

12
32
12
42
343
hello dear
how is life
it is good man
Yes, it is the one
Have been search for this
Really, it is the only one

and trying to save the output to another text file(output.txt) using the windows command
new_scan.exe <input.txt> output.txt in windows it is showing new_scan.exe has stopped working.

Possibly this is due to segmentation fault but I could not figure it out. Please help to resolve it.

Thanks in advance!

Upvotes: 1

Views: 134

Answers (2)

Utkal Sinha
Utkal Sinha

Reputation: 1041

First of all you need to increase your array size because some of your test cases are more than 20 bytes.

Second since you are reading from a file so you need to check for EOF. So change

while (c != '\n'){
//  Do some work
}

To

while (c != '\n' && c != EOF){
// Do some work.
}

Lastly, you need to free your allocated memory for good practices.

Now your code should work fine.

Upvotes: 4

0xF1
0xF1

Reputation: 6116

Your array size is 20 bytes but few sentences in the text file are more than 20 characters (bytes) in length.

Accessing the unallocated memory might be causing SEG-Fault.

Another issue that can be there, is the end of line delimiter. You have checked for \n as EOL, but it can be of the form \r\n (CR-LF combination). So You shoul check for \r also as the delimiter.

Also, a suggestion for you, You must free the memory you allocate using 'alloc family of functions.

Upvotes: 2

Related Questions