简体   繁体   English

输入大小为 1 的字符串时出现分段错误

[英]Segmentation Fault on input of string of size 1

I am getting segmentation fault (core dumped) when i pass string of size 1 as input.当我将大小为 1 的字符串作为输入传递时,出现分段错误(核心转储)。 I tried searching for the reason but i could not find any , i was asked to debug my program .我试图寻找原因,但找不到任何原因,我被要求调试我的程序。 From debugging i have realised that it throws segmentation fault on reading string of size 1.从调试中我意识到它在读取大小为 1 的字符串时会引发分段错误。

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    ll t,x,y;
    string s;
    cin>>t;
    while(t--){
        cin>>s;
        int tilt=0;
        for(int i=s.length()-1,j=0;i>=s.length()/2;i--,j++){
            if(s[i]!=s[j]){
                tilt = s[i]>s[j]?-1:1;
                s[i]=s[j];
            }
        }
        if(tilt==-1 || tilt==0){
            if(s.length()%2==0){
                y=1;x=s.length()/2-1;
                while(y!=0 && x>=0){
                    if(s[x]=='9'){
                        s[x]='0';
                    }else{
                        s[x] += 1;
                        y=0;
                    }
                    x--;
                }
                if(y==0){
                    for(int i=0;i<s.length()/2;i++){
                        cout<<s[i];
                    }
                    for(int i=s.length()/2-1;i>=0;i--){
                        cout<<s[i];
                    }
                    cout<<endl;
                }else{
                    cout<<1;
                    for(int i=0;i<s.length()/2;i++){
                        cout<<s[i];
                    }
                    for(int i=s.length()/2-2;i>=0;i--){
                        cout<<s[i];
                    }
                    cout<<1<<endl;
                }
            }else{
                y=1;x=s.length()/2;
                while(y!=0 && x>=0){
                    if(s[x]=='9'){
                        s[x]='0';
                    }else{
                        s[x] += 1;
                        y=0;
                    }
                    x--;
                }
                if(y==0){
                    for(int i=0;i<s.length()/2;i++){
                        cout<<s[i];
                    }
                    for(int i=s.length()/2;i>=0;i--){
                        cout<<s[i];
                    }
                    cout<<endl;
                }else{
                    cout<<1;
                    for(int i=0;i<s.length()/2;i++){
                        cout<<s[i];
                    }
                    for(int i=s.length()/2-1;i>=0;i--){
                        cout<<s[i];
                    }
                    cout<<1<<endl;
                }
            }
        }else{
            cout<<s<<endl;
        }
    }
    return 0;
}

Input: 1 11 Output: 22输入:1 11 输出:22

Input: 1 1 Output: Error输入:1 1 输出:错误

Question : https://www.spoj.com/problems/PALIN/问题: https : //www.spoj.com/problems/PALIN/

Here这里

for(int i=s.length()-1,j=0;i>=s.length()/2;i--,j++){

when s is of size 1, s.length()/2 is an unsigned quantity with value 0. When comparing an integer with an unsigned, the integer is converted to an unsigned before the comparison.s的大小为 1 时, s.length()/2是值为 0 的无符号量。当将整数与无符号数进行比较时,在比较之前将整数转换为无符号数。 Checking if an unsigned value is >= 0 is always true, so this is an infinite loop.检查无符号值是否 >= 0 始终为真,因此这是一个无限循环。 That's why it crashes.这就是它崩溃的原因。

I would hope that your compiler gives you warnings about the dangers of signed/unsigned comparisons.我希望您的编译器向您发出有关有符号/无符号比较危险的警告。

Here's one way to fix this particular loop, but I'd guess you have similar problems elsewhere in your code.这是修复此特定循环的一种方法,但我猜您在代码的其他地方也有类似的问题。

for (size_t i = s.length(), j = 0; i > s.length()/2; ++j) {
    --i;
    if (s[i] != s[j]) {
        tilt = s[i] > s[j] ? -1 : 1;
        s[i] = s[j];
    }
}

Some advice一些忠告

  • Use size_t for index variables.size_t用于索引变量。
  • When looping backwards be very careful about the termination condition, the way I do it is to make the variable one bigger than it needs to be and decrement it at the beginning of the loop instead of the end.向后循环时要非常小心终止条件,我这样做的方法是使变量比它需要的大一个,并在循环的开始而不是结束时递减它。 That way you compare with > not >= and avoid the problem your code has.这样您就可以与> not >=进行比较并避免您的代码出现的问题。
  • Try to use some spaces in your code.尝试在代码中使用一些空格。 It makes it easier to read for everyone, including yourself.它使每个人都更容易阅读,包括您自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM