0

I am aware about the redundancy of the argument. Because of that, I would like to thank you all in advance for your patience.

I have thoughtfully read many good thread about the issue such as this and this. I have conceptually clear what core dumping is, but I cannot figure out what practically is and how to detect it.

Please consider the following example I am working on: it is a simple program that checks if an input string is a palindrome:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 20

int length(char s[SIZE]){
        int i;
        while(s[i] != 0)
                i++;
        return i;
}

int main(){
        char s[SIZE], c;
        int i = 0, j, flag = 1;
        printf("\n\nGood morning, master. Tell me a word. I will check if it is a\n"
                "palindrome\n\n");
        for(i = 0; (c = getchar()) != '\n'; ++i){
                s[i] = c;
        }
        j = length(s) - 1;
        while(flag && i < j){
                flag = s[i] == s[j];
                i++; j--;
        }
        if(flag)
                printf("\n\nSuccess!\n\n");
        return 0;
}

What does cause in this program "the core to bump?"

Worice
  • 3,847
  • 3
  • 28
  • 49
  • doubt that code will dump core - it won't even compile – Chris Turner May 22 '17 at 13:20
  • `char c[SIZE];` ----> `char s[SIZE]; char c;` – LPs May 22 '17 at 13:21
  • Actually on my machine compiles (gcc compiler on lubuntu VM). – Worice May 22 '17 at 13:22
  • 1
    You didn't post your real code, obviously. The posted code does not defines `s` buffer...for example – LPs May 22 '17 at 13:22
  • Why are you creating your own `strlen` function? And why aren't you terminating the input string (which is the likely cause of the crash)? – Some programmer dude May 22 '17 at 13:22
  • 1
    The core being dumped is not the problem, it's the OS' way of helping you deal with the problem. "Core" means "contents of memory", i.e. your program does something invalid which causes the OS to kill it, while saving the current state of the program (the "core dump") so that you have something to debug later. – unwind May 22 '17 at 13:22
  • @LPs that is my code. I am working on it right now. – Worice May 22 '17 at 13:23
  • @Worice Lol, take a look [Here](http://ideone.com/lNTUHq) so..... moreover your `length` function does not return.... – LPs May 22 '17 at 13:24
  • 1
    Ignoring issues of whether this code can even be compiled, the obvious reasons for this program to dump core are that both the loop in `length` and the first loop in `main` do not stop at the end of the array. If the termination condition isn't met before they reach the end of mapped memory, the operating system will fire a segfault. There are many other bugs as well. – zwol May 22 '17 at 13:24
  • @LPs ahah I had a core bump! – Worice May 22 '17 at 13:25
  • I wrote two typos in the code. I am sorry. Now it should compile. – Worice May 22 '17 at 13:33
  • @Worice You forgot a `;` after `c`... ;) – LPs May 22 '17 at 13:36

2 Answers2

2

Your length function has 3 big problems:

  1. local i is not initialized to
  2. The function must return i value at the end.
  3. You must check to access your s array in-bounds

int length(char s[SIZE]){
        int i = 0;
        while ((s[i] != 0) && (i<size-1))
                i++;
    return i;
}

You must take care of s length in your "character collecting loop" too. You must take care of EOF (that can be returned by getchar, for example using CTRL + D on Linux) and you must terminate your string. C-Strings are nul-terminated

for (i = 0; (((c = getchar()) != '\n') && (c!=EOF)&& (i<SIZE-2)); ++i)
{
    s[i] = c;
}
s[i] = '\0';

Finally take note that your final loop does not what you guess, in only return if there are 2 same chars in the input string...

LPs
  • 16,045
  • 8
  • 30
  • 61
  • Please don't answer questions like this unless you are prepared to take the time to go through the code line by line and explain _all_ of the mistakes. – zwol May 22 '17 at 13:34
  • @zwol I was updating the answer, I forgot something? – LPs May 22 '17 at 13:35
  • I see at least three bugs in `main`, some of which could cause a segfault; also, "You have reinvented `strlen` poorly" should be mentioned as a problem with `length` ;-) – zwol May 22 '17 at 13:38
  • @zwol Yes, you right I edited ;) – LPs May 22 '17 at 13:47
  • One more: the `getchar` loop needs to check for `EOF` as well as `'\n'`. (I'm not going to go through the whole program line by line myself, but that one is a very common mistake that can lurk for years before it makes something go into an infinite loop, so it really should be called out whenever it happens.) – zwol May 22 '17 at 13:54
  • @zwol You're (obviously) right, edited. – LPs May 22 '17 at 14:24
1

You have not initialized i in length function, so its value can be anything

If its value is greater than SIZE you will end up accessing invalid memory, leading to segment fault

Pras
  • 4,047
  • 10
  • 20
  • Please don't answer questions like this unless you are prepared to take the time to go through the code line by line and explain _all_ of the mistakes. – zwol May 22 '17 at 13:34