我在以下代码中收到总线错误
I am getting a bus error in the following code
我的代码中出现总线错误。使用此代码,我正在尝试将数字转换为单词,但我知道我的逻辑存在缺陷。但在此之前,当我在 Mac 上使用 g++ 编译和运行此代码时,我正在尝试使此代码按原样运行,并且出现总线错误。任何帮助将不胜感激。
当我运行代码时,我得到以下输出。我有调试消息来跟踪错误发生的位置。
输入数字:1234 主 1:数字是:234 函数1:数字为234 二 两百 34函数2:数字为34 功能3:数字为34 总线错误:10
#include <iostream>
#include <string>
using namespace std;
char *convert_number(int);
char *tens[] = {"", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"};
char *words[] = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "ninteen"};
char *place[] = {"", "hundred", "thouands", "million", "billion", "trillion"};
int main(int argc, char **argv)
{
int number, conv_num, places;
places = 1;
char *string = new char[1000];
char *temp_string = new char[100];
cout << "Enter a number:";
cin >> number;
string = " ";
if (number >= 1000)
{
while(number >= 1)
{
conv_num = number % 1000;
cout << "main 1:numbers are:" << conv_num << endl;
temp_string = convert_number(conv_num);
string =strcat(string, temp_string);
string =strcat(string, " ");
number = 0; // (number-conv_num)/1000;
cout << "main 2:numbers are:" << endl;
//cout << conv_num << ":" << number << endl;
}
}
else
{
string = convert_number(number);
string = strcat(string, " ");
}
cout<<"Main: The word is :"<<string<<endl;
}
char *convert_number(int number)
{
int divisor;
char *word;
word = new char[100];
divisor = 10;
cout << "Function1: Number is " << number << endl;
if (number >= 100)
{
word = strcat(word, words[number/100]);
cout << word << endl;
word = strcat(word, " hundred ");
cout << word << endl;
number = number%100;
cout << number;
}
cout << "Function2: Number is " << number << endl;
if (number >= 20)
{
word = strcat(word, tens[number/10]);
word = strcat(word, " ");
if (number%divisor >= 1)
{
word=strcat(word, words[number%divisor]);
word =strcat(word, " ");
}
}
cout << "Function3: Number is " << number << endl;
if (number < 20)
{
word = strcat(word, words[number]);
word = strcat(word, " ");
}
cout << "Returning word:" << word;
return word;
}
您收到总线错误的原因是因为您尝试写入不可写区域(即写入字符常量,并且还超过其末尾);这是未定义的行为。
// Good: allocate 100 bytes to string
char *string = new char[100];
// Bad! Compiler warns you that assigning character const to char* is wrong.
// It does not tell you that you've just leaked the 100 bytes that you allocated
/// before, but that's also true.
string=" ";
// ... some more code, and then
string = strcat(string,temp_string); // <<== HERE is the problem!
对strcat
的调用试图写入string
的终止零,然后继续写入字符串常量的末尾。这是未定义的行为,因此程序崩溃。
要解决此问题,您需要将" "
复制到 string
中,而不是将其分配给string
指针。
相关文章:
- 总线错误,堆上分配的内存
- 总线错误 10 - 字符 * 数组
- C/C++ 字符串错误与 ARM SEG 故障总线错误
- 获取总线错误:10 并附加字符串
- 多维矢量总线错误
- 链表的总线错误(核心转储)
- C 结构中的总线错误
- 如何解决此总线错误
- 调用虚拟方法导致总线错误
- 尝试打印数组时出现总线错误和奇怪的输出
- 尝试在共享内存中写入时发生总线错误
- C++ 总线错误:尝试解析和更新字符串时出现 10
- 我在以下代码中收到总线错误
- 总线错误:10 还有更多索引要去
- 使用链表的动态队列//总线错误:10(C++)
- 快速修复程序给出的总线错误
- 总线错误10或分段错误11
- C++ 在类成员"无符号长"上使用"^="和"<<"时出现总线错误
- 析构函数中的模拟总线错误
- 试图获取vector迭代器的值时出现总线错误10