[英]Why am I getting "runtime error: Segmentation fault" for random test cases?
I'm asked to check whether a given string can be a palindrome after rearranging and then return true if it can be a palindrome or false if it cannot be a palindrome.我被要求检查给定的字符串在重新排列后是否可以是回文,然后如果它可以是回文则返回 true,如果不能是回文则返回 false。
I'm getting a runtime error: Segmentation fault while running tests.我收到运行时错误:运行测试时出现分段错误。
Here's my code:这是我的代码:
bool palindromeRearranging(char * inputString) {
int index;
int count, count1 = 0;
for(int i = 0, b = strlen(inputString); i < b -1 ; i++)
{ count = 0;
if(inputString[i] == '*')
{
continue;
}else
{
for(int j = i+1; j < b ;j++)
{
if(inputString[i] == inputString[j] )
{
inputString[j] = '*';
count++;
index = i;
}
}
inputString[index] = '*';
}
if(count %2 == 0 && count != 0)
{
count1++;
}
}
for(int i = 0, b = strlen(inputString); i < b; i++)
{
if(inputString[i] != '*')
{
count1++;
}
}
if(count1 > 1)
{
return false;
}else
{
return true;
}
}
Here inputString
is the string given.这里
inputString
是给定的字符串。 I tried to replace the two characters with *
if they are both same.如果它们都相同,我尝试用
*
替换这两个字符。 And then counting the no.然后数数。 of single characters.
的单个字符。
5 out of 10 test cases have passed. 10 个测试用例中有 5 个通过。
For example test cases like "abbcabb"
, "aabb"
, "zyyzzzzz"
have passed.例如,像
"abbcabb"
、 "aabb"
、 "zyyzzzzz"
这样的测试用例已经通过。
But I'm getting runtime error: Segmentation fault for test cases like "abcad"
, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbccccaaaaaaaaaaaaa"
.但我收到运行时错误:
"abcad"
、 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbccccaaaaaaaaaaaaa"
等测试用例的分段错误。 "abca"
, "abdhuierf"
. "abca"
、 "abdhuierf"
。
I hope you could help me with this one.我希望你能帮我解决这个问题。
Btw I'm solving this on codesignal.com, under the arcade section/intro.顺便说一句,我正在街机部分/介绍下的 codesignal.com 上解决这个问题。 Q.18 palindromeRearranging.
Q.18回文重新排列。
As it was already mentioned you are using the non-initialized variable index.正如已经提到的,您正在使用未初始化的变量索引。
int index;
in this for loop在这个 for 循环中
for(int i = 0, b = strlen(inputString); i < b -1 ; i++)
{ count = 0;
if(inputString[i] == '*')
{
continue;
}else
{
for(int j = i+1; j < b ;j++)
{
if(inputString[i] == inputString[j] )
{
inputString[j] = '*';
count++;
index = i;
}
}
inputString[index] = '*';
}
if in the inner for loop of the else statement a repeated character was not found that results in undefined behavior. if 在 else 语句的内部 for 循环中没有找到导致未定义行为的重复字符。
Or you can use an invalid value of the variable index that it keeps after a previous iteration of the loop.或者,您可以使用在循环的前一次迭代之后保留的变量索引的无效值。
Also not all characters that satisfy the condition也不是所有满足条件的字符
if(inputString[i] == inputString[j] )
are substituted for the character '*'.替换为字符“*”。
But in any case your approach is invalid.但无论如何,你的方法是无效的。 You shall not change the original string.
您不得更改原始字符串。 Otherwise after calling your function the caller will deal with a modified string.
否则,在调用您的 function 后,调用者将处理修改后的字符串。
The function can be written for example the following way as it is shown in the demonstrative program below.例如,function 可以按以下方式编写,如下面的演示程序所示。
#include <stdio.h>
int can_be_palindrome( const char *s )
{
size_t odd = 0;
for ( const char *p = s; odd < 2 && *p; ++p )
{
const char *q = s;
while ( q != p && *q != *p ) ++q;
if ( q == p )
{
size_t n = 1;
while ( *++q )
{
if ( *q == *p ) ++n;
}
odd += n % 2;
}
}
return odd < 2;
}
int main(void)
{
const char *s = "abbcabb";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
s = "aabb";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
s = "zyyzzzzz";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
s = "abca";
printf( "\"%s\" can be a palindrome is %s\n",
s, can_be_palindrome( s ) ? "true" : "false" );
return 0;
}
The program output is程序 output 是
"abbcabb" can be a palindrome is true
"aabb" can be a palindrome is true
"zyyzzzzz" can be a palindrome is true
"abca" can be a palindrome is false
"Why am I getting “runtime error: Segmentation fault” for random test cases?"
“为什么随机测试用例会出现“运行时错误:分段错误”?”
Aside from your function not being logically viable for testing palindromes, it will fail on random occasions due to undefined behavior :除了您的 function 在逻辑上不能用于测试回文之外,由于未定义的行为,它会在随机情况下失败:
int index;
Is not initialized,未初始化,
It fails here for example when its value exceeds the array index:例如,当它的值超过数组索引时,它在这里失败:
inputString[index] = '*';
With the following sequence of run-time messages:使用以下运行时消息序列:
To address this, change this:为了解决这个问题,改变这个:
int index;
int count, count1 = 0;
to this:对此:
int index = 0;
int count = 0, count1 = 0;
Aside, here is an example of a similar question and answer.另外, 这里有一个类似问题和答案的例子。
Error is due to unintialized behaviour of your int index;
错误是由于您的
int index;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.