C ++ random 0xC0000005错误 [英] C++ random 0xC0000005 errors
问题描述
我制作了一个将 n 十进制数字 sk 转换为其他数字系统 p 的程序,但有时它会崩溃,我得到的错误代码是0xC0000005(程序仍然转换并输出所有数字)。一个事情我只是注意到它发生,然后转换数字超过6个符号(或者只是一个巧合)。
#include < iostream>
#include< fstream>
#include< string>
using namespace std;
int main()
{
long n,sk,p,j;
string liekanos;
ifstream f(u1.txt);
f>> n;
for(int i = 0; i {
f> sk>> p;
j = 0;
while(sk> 0)
{
liekanos [j] = sk%p;
sk / = p;
j ++;
}
for(j> = 0; j--;)
{
if(liekanos [j] <10)
cout < liekanos [j]);
else cout<< char(liekanos [j] +55);
}
cout<< endl;
}
return 0;
}
输入示例:
3
976421618 7
15835 24
2147483647 2
$ b 使用 liekanos [j]
您访问索引时的元素
,但由于你没有指定这个字符串的大小,你很可能试图访问非现有的元素。你可以在输入和
之前调用 liekanos.resize(sk)
,以确保不会发生。
或者,如果你知道 liekanos
的最大大小,你可以声明为 string liekanos (N,c);
其中 N
是其大小, c
是默认值的每个字符。
I made a program which converts n decimal numbers sk into other numerical system p but sometimes it crashes and the error code I get is 0xC0000005 (program still converts and outputs all the numbers) . One thing I just noticed that it happens then converted number is longer than 6 symbols (or it's just a coincidence).
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
long n,sk,p,j;
string liekanos;
ifstream f("u1.txt");
f >> n;
for (int i=0;i<n;i++)
{
f >> sk >> p;
j=0;
while (sk>0)
{
liekanos[j]=sk % p;
sk/=p;
j++;
}
for (j>=0;j--;)
{
if (liekanos[j]<10)
cout<<int(liekanos[j]);
else cout<<char(liekanos[j]+55);
}
cout<<endl;
}
return 0;
}
Example input:
3
976421618 7
15835 24
2147483647 2
With liekanos[j]
you access element at index j
but since you haven't specify size of this string, you are most likely trying to access non-existing element. You could call liekanos.resize(sk)
before you enter your while
loop to make sure it never happens.
Or if you know the maximum possible size of liekanos
, you could declare it as string liekanos(N, c);
where N
is its size and c
is the default value of each character in it.
这篇关于C ++ random 0xC0000005错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!