The code compiles properly with online gdb compiler but here it gives an error of segmentation fault

I developed a code for the exercise of isogram. The codes compiles perfectly with online gdb compiler but here it creates an error of segmentation fault, I don’t understand why. I am attaching my code below for reference and requesting for some support here.

#include "isogram.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool is_isogram(char phrase[])
{
    int is_char_uniq=0;
    int len_of_strin = strlen (phrase);

    for (int i=0; i< len_of_strin; i++)
    {
        for (int j=i; j<len_of_strin; j++)
        {
            if (j!=i)
            {
                if (phrase[i] != phrase[j])
                {
                    is_char_uniq =1;
                }
                else
                {
                    is_char_uniq =0;
                    return false;
                }
            }
        }
    }

    if (is_char_uniq == 1)
    {
        return true;
    }

    return false;
}

[Edited by admin to add codeblock]

If there’s an error message from the compiler, that would be useful to include, too!

Also, you can put all your code (and error messages) in a codeblock!

```cpp
Your code here
for (int i=0; i< len_of_strin; i++)
```

renders as

Your code here
for (int i=0; i< len_of_strin; i++)

Thanks for the tips. The error message is as below :

make: *** [makefile:22: test] Segmentation fault (core dumped)

For phrase “apple”, what is the maximum value of i? What is at phase[i] for that maximum value?

Have you replicated the segmentation fault? I’m failing to.

Take a look at the second test:

static void test_null(void)
{
   TEST_IGNORE();   // delete this line to run test
   TEST_ASSERT_FALSE(is_isogram(NULL));
}

It passes NULL to the function and expects the result to be false.
Does your solution handle this NULL correctly?

There are a few other issues, too, but those should be easier to solve.
Let me know if you have questions or need hints or help. Cheers!

if (strlen (phrase)<=0)
        return false;

To handle the NULL I added the above statements to my code still it gives me the same error

strlen() requires a null-terminated string.
NULL is not a null-terminated string.

You have to handle NULL in a different way.

I am out of ideas right now to solve this. Can you help me?

You could just handle NULL as a special case:

if (phrase == NULL)
    return false;

Still the same error bruh!!!

@Bhavin-Sharma

  • Do you know what a segmentation fault is?
  • Do you know what NULL is?
  • Can you explain the difference between NULL and an empty string?

To be able to fix your problem, you need to understand all of the above.

If you run make memcheck instead of make test, the error will be more informative.


Please keep in mind that these people are trying to help you.

If you show me the code and the error message I will try my best to help you.

Edit:

I just took the code that you posted above and inserted my two lines at the beginning of the function. The segmentation fault disappeared, there are now more “normal” test failures.

Welcome @Bhavin-Sharma.

In case you replaced the contents of the previous expression, i.e.

if (strlen (phrase)<=0)
        return false;

with this:

if (phrase == NULL)
    return false;

then try adding the second snippet above the first. You need both :wink:

The second one is necessary because in C, arrays are referenced by pointer (i.e. memory address), and dereferencing a pointer of address 0x0 (NULL) (which happens in strlen) causes the processor to “panic”, and run a special procedure of the operating system that closes down the program. So while the NULL-pointer is a legal type of argument, it is not a valid address, so the program crashes on runtime. In C we use NULL for something uninitialized and/or error control flow. I hope that makes sense :slight_smile:

Edit: You can combine them though, such as:

if (!phrase || strlen(phrase) == 0)
    return false;

In the above if-statement, the first part of the expression is evaluated first, and the second is only evaluated if the first one is false - so it should be safe. phrase is casted to true if it is non-NULL, so we just negate the result with !. Also, no need to check if the string length is less than zero.