Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我在尝试向函数传递字符时会得到SIGSEGV,以及如何修复它?_C++_Segmentation Fault - Fatal编程技术网

C++ 为什么我在尝试向函数传递字符时会得到SIGSEGV,以及如何修复它?

C++ 为什么我在尝试向函数传递字符时会得到SIGSEGV,以及如何修复它?,c++,segmentation-fault,C++,Segmentation Fault,我正在类中重载运算符=,我需要确定字符串是否包含数字。不幸的是,我主要需要使用C数组(char*),原因有二: 训练 使用std::string需要我更改100多行代码 当尝试将值从char*数组传递到我的函数时,我得到了一个非常好的SEGFAULT,不幸的是,我不知道为什么。可能我没有正确使用指针 我搜索了几个小时,但没能找到解决这个问题的办法。不管怎样,这是代码。如果你需要更多关于这个问题的信息,请在评论中告诉我 #include <limits.h> #inclu

我正在类中重载
运算符=
,我需要确定字符串是否包含数字。不幸的是,我主要需要使用C数组(
char*
),原因有二:

  • 训练
  • 使用std::string需要我更改100多行代码

    当尝试将值从
    char*
    数组传递到我的函数时,我得到了一个非常好的SEGFAULT,不幸的是,我不知道为什么。可能我没有正确使用指针

我搜索了几个小时,但没能找到解决这个问题的办法。不管怎样,这是代码。如果你需要更多关于这个问题的信息,请在评论中告诉我

 #include <limits.h>
    #include <iostream>
    #include <string.h>
    #include <exception>

    struct notdig : public std::exception {
    virtual const char* what() const throw() override{
      return "int_huge: isdig(*(o + i)) returned false. \nYour declaration of an int_huge integer should contain decimal digits.";
    }
}; 
class int_huge {
    private:
        char* buffera;                   
        char* bufferb;
        int numDig(unsigned long long int number){ //Gets the number of digits
            int i = 0;
            while(number){
                number /= 10;
                i++;
            }
            return i;
        }
        inline bool isdig( char character ) { //Checks whether character is digit or not
            return ( '0' <= character && character <= '9' );
        }
    public:
        int_huge(){
           this->buffera = "0"; 
           this->bufferb = "0";
        }
        void operator=(char* operand){
            for (int i  = 0; i < strlen(operand); i++){
                if (!isdig(operand[i])){
                    throw notdig();
                }
            }
            if (strlen(operand) >= numDig(ULLONG_MAX)){
                if (strlen(operand) - numDig(ULLONG_MAX)){ //Equivalent with if (strlen(operand) != numDig(ULLONG_MAX)
                    int i = 0;
                    while (i < strlen(operand)-numDig(ULLONG_MAX)){
                        this->buffera[i] = operand[i];
                        i++;
                    }
                    this->bufferb = operand + i;
                } else {
                    this->buffera[0] = operand[0];
                    this->bufferb = operand + 1;
                }
            } else {
                this->buffera = "0";
                this->bufferb = operand;
            }
        }
    };




    int main() {
        int_huge object;
        try {
            object = "90";
        } catch (std::exception &e) {
            std::cout << e.what();
        }
    }
#包括
#包括
#包括
#包括
struct notdig:public std::exception{
虚拟常量char*what()常量throw()覆盖{
return“int_巨整数:isdig(*(o+i))返回false。\n int_巨整数的声明应包含十进制数字。”;
}
}; 
国际大类{
私人:
char*buffera;
char*bufferb;
int numDig(无符号长整型数){//获取位数
int i=0;
while(数字){
数目/=10;
i++;
}
返回i;
}
内联bool isdig(字符){//检查字符是否为数字
返回('0'bufferb=“0”;
}
void运算符=(字符*操作数){
for(int i=0;i=numDig(ULLONG_MAX)){
if(strlen(操作数)-numDig(ULLONG_MAX)){//与if(strlen(操作数)!=numDig(ULLONG_MAX)等价
int i=0;
while(ibuffera[i]=操作数[i];
i++;
}
this->bufferb=操作数+i;
}否则{
这->缓冲区A[0]=操作数[0];
this->bufferb=操作数+1;
}
}否则{
这->buffera=“0”;
此->缓冲区B=操作数;
}
}
};
int main(){
大型物体;
试一试{
object=“90”;
}捕获(标准::异常&e){
std::coutIn

main
at

o = "90";
,它可能不在可写存储器中,被分配给指向
char
的非常量指针。如果编译器支持C++11或更新的标准,编译器应就此向您发出警告,或干脆拒绝编译

operator=
中爆炸:

this->a[i] = o[i];
在这里:

this->a[0] = o[0];
当程序试图写入无法写入的存储器时

解决方案:

使用
std::string
。如果这不在表中,而且听起来像是这样,请不要使用字符串文字。使用
new
在可写内存中分配一个缓冲区,将文字复制到缓冲区,然后分配缓冲区。还要记住,缓冲区的大小是固定的。尝试在缓冲区中放置较大的字符串将以d结尾伊斯特


这将是一个令人头痛的内存管理问题和令人恐惧的演示,因此请小心。

您能演示调用赋值运算符的代码吗?1)您是否在观察变量值时尝试使用调试器单步执行代码?2)请提供(请注意,您不需要提供所有代码—只需制作最小但完整的示例即可)。发布a,而不是较大程序的一部分(较大程序控制整个节目)@drescherjm如果是这样的话,你会期望在第一次迭代发生之前在
strlen
上发生崩溃。但是当然,OP可能会误诊问题。不幸的是,我需要使用C数组——这并不妨碍你使用更短的东西,比如--
if(!std::all_of(o,o+strlen(o),::isdigit))throw;
非常感谢您的帮助:)你能给我指出一些东西吗?可以写不能写的存储,导致意外的行为吗?@ DeMeReSoCurror它肯定会在C程序中引起。C++在类型检查上更苛刻,所以我也期待相同。不能引用一章和一节来给你,想啊。好了,现在我明白了。非常感谢。
this->a[i] = o[i];
this->a[0] = o[0];