[英]C++ program out of memory
首先,我真的很抱歉,但我不知道我的程序中的错误在哪里,我已经经历了数百次,但我找不到它。
当我在VB2010中进行调试时,我遇到了访问验证错误,因此必须有一个函数从某个不允许的地方获取内存。 程序用于从数据库中获取一些数据,对其进行排序并将其返回到output.txt。
它是拉丁语 - 克罗地亚语字典。 无论如何请帮忙。
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <string>
#include<stdlib.h>
using namespace std;
void ascii_convert(int);
void string_compare(string);
void word_sort(int a,string inp);
string word1=0;
string word2=0;
string word3=0;
string upis;
string word[30] = {};
int match=0;
int input=0;
int broj_rijeci = 0;
int main()
{
string line;
ifstream myfile ("database.txt"); //loading database.txt
int count = 0;
if (myfile.is_open()){
while ( getline (myfile,line) ){
if(line.at(0) == '['){ //get's the number of words in database..
int i = line.length()-1; //...that is the first line of databse
int brojac = 0;
while(line.at(i) != line.at(0)){
input = line.at(i);
ascii_convert(input);
broj_rijeci = broj_rijeci + input*static_cast<int>(pow(static_cast<double>(10),brojac));
i--;
brojac++;
}
}
if(line.at(0) != '['){
for(int i = 0; line.at(i)!=' ' ;i++){
word[count] = word[count] + line.at(i);
}
count++;
}
}
myfile.close();
}
else cout << "Unable to open file";
for(int i = 0; i<broj_rijeci ;i++) cout<<word[i]<<endl;
cin>>upis;
string_compare(upis); //after this line I will put the output as output.txt
system("pause");
return 0;
}
void string_compare(string){
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word[i].length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word[i].at(y)) match++;
}
word_sort(match,word[i]);
}
}
void word_sort(int a,string inp){
int match1 = 0;
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word1.length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word1.at(y)) match1++;
}
}
if(a>match1){
word3 = word2;
word2 = word1;
word1 = inp;
}else{
int match1 = 0;
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word2.length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word2.at(y)) match1++;
}
}
if(a>match1){
word3 = word2;
word2 = inp;
}else{
int match1 = 0;
int len1 = upis.length();
int len;
for(int i = 0; i < broj_rijeci; i++){
int len2 = word3.length();
if (len1<len2) len = len1;
else len = len2;
for(int y = 0; y < len; y++){
if(upis.at(y) == word3.at(y)) match1++;
}
}
if(a>match1) word3 = inp;
}
}
}
void ascii_convert(int){ //function for converting variable "input"
if(input == 48){ //from ascii to decimal numbers
input = 0;
}
if(input == 49){
input = 1;
}
if(input == 50){
input = 2;
}
if(input == 51){
input = 3;
}
if(input == 52){
input = 4;
}
if(input == 53){
input = 5;
}
if(input == 54){
input = 6;
}
if(input == 55){
input = 7;
}
if(input == 56){
input = 8;
}
if(input == 57){
input = 9;
}
}
这是我的“database.txt”:
[4]
terra ae f zemlja
amica ae f prijateljica
puela ae f djevojcica
nauta ae m mornar
谢谢你的支持
首先,所有这些都必须修复
string word1=0;
string word2=0;
string word3=0;
这些是将string
对象初始化为被解释为转换的NULL指针的对象。 不好。 坦率地说,我很震惊你的程序甚至在main()
之前完成初始化全局变量,因为它肯定不在我的工具链上。
接下来,这是使用远远超出其数组声明大小的索引:
for(int i = 0; i<broj_rijeci ;i++) cout<<word[i]<<endl;
使用给定的输入文件并使用您的加载算法, broj_rijeci
计算为133,只有94个条目长于word[]
的大小。 总之,你的该变量的计算是平错误的,或阵列是方式矮小。 无论哪种方式,其未定义的行为 。 我倾向于那些前者(负载算法是错误的)。 但是,如果没有关于每一行每个实体目的的准确信息,那么我就可以在这方面提供更多的投入。
除此之外,这充满了编程的不稳定性和糟糕的做法。 函数传递参数只是为了修改传递的参数的全局变量只是冰山一角。
克罗地亚语 - 英语备忘单
对于想要扩展该算法的相关方(或者至少尝试并理解它尝试做什么):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.