程序错误 ntdll!RtlQueryPerformanceCounter()
Program error ntdll!RtlQueryPerformanceCounter()
当我运行这段代码时:
//============================================================================
// Name : sum.cpp
// Author : Yordan Borisov
// Version :
// Copyright : MIT License
//
// Description : Sum bug numbers in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int _INT(char ch) {
return ch - '0';
}
char _CHAR(int n) {
return n + '0';
}
char* validateSize(int size_one, int size_two, char* _n_T, char*& _n_O) {
if (size_one != size_two) {
int diff = abs(size_one - size_two);
int _less_Length = strlen((size_one > size_two ? _n_T : _n_O));
char* tmp = new char[_less_Length + diff];
strcpy(tmp, "[=10=]");
int _index = 0;
for (int i = 0; i < diff; i++) {
tmp[_index++] = '0';
}
tmp[_index] = '[=10=]';
for (int i = 0; i < _less_Length; i++) {
tmp[_index++] = (size_one > size_two ? _n_T : _n_O)[i];
}
tmp[_index] = '[=10=]';
(size_one > size_two ? _n_T : _n_O) = tmp;
}
return _n_T;
}
char* sum(const char* numOne, const char* numTwo) {
int size_one = strlen(numOne);
int size_two = strlen(numTwo);
char* _n_O = new char[size_one];
char* _n_T = new char[size_two];
strcpy(_n_O, numOne);
strcpy(_n_T, numTwo);
_n_T = validateSize(size_one, size_two, _n_T, _n_O);
char* result = new char[size_one + 1];
result[size_one] = '[=10=]';
int _CF = 0;
int _increase = 0;
for (long i = (size_one > size_two ? size_one : size_two) - 1; i >= 0; i--) {
char chOne = _n_O[i];
char chTwo = _n_T[i];
int _sum = _INT(chOne) + _INT(chTwo) + _CF;
_CF = _sum / 10;
result[i] = _CHAR(_sum % 10);
if (i == 0 && _CF > 0) {
//in that case additional element in the result must be added.
char _ch = _CHAR(_CF);
char* _tmp_Pr = new char[1];
_tmp_Pr[0] = _ch;
_tmp_Pr[1] = '[=10=]';
_increase = 1;
char* _tmp_R = strcat(_tmp_Pr, result);
strcpy(result, _tmp_R);
}
}
result[(size_one > size_two ? size_one : size_two) + _increase] = '[=10=]';
return result;
}
int main(int argc, char* argv[]) {
cout
<< sum(
"147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214",
"3652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144")
<< endl;
return 0;
}
我遇到了这个异常:
Thread #1 0 (Suspended : Signal : SIGSEGV:Segmentation fault)
ntdll!RtlQueryPerformanceCounter() at 0x777531f6
ntdll!RtlQueryPerformanceCounter() at 0x77753431
ntdll!RtlpNtEnumerateSubKey() at 0x777f0f0e
ntdll!RtlUlonglongByteSwap() at 0x777aaa86
0x740000
ntdll!RtlQueryPerformanceCounter() at 0x77753431
towlower() at 0x7700ade8
towlower() at 0x7700ae43
msvcrt!_close() at 0x77013bb9
0x1
<...more frames...>
在第777764bc行的反汇编代码中:
......... ...
777531c2: dec %esp
777531c3: add %dh,-0x75(%esi,%edx,1)
777531c7: dec %eax
777531c8: push %eax
777531c9: xor %ecx,(%esi)
777531cb: mov 0x2(%esi),%cl
777531ce: xor 0x1(%esi),%cl
777531d1: xor (%esi),%cl
777531d3: cmp %cl,0x3(%esi)
777531d6: jne 0x777aab1a <ntdll!RtlUlonglongByteSwap+56842>
777531dc: movzwl (%esi),%ebx
777531df: cmp -0x2c(%ebp),%ebx
777531e2: jb 0x77794568 <ntdll!RtlGetProcessHeaps+199>
777531e8: lea 0x8(%esi),%ecx
777531eb: mov (%ecx),%edi
777531ed: mov %edi,-0x48(%ebp)
777531f0: mov 0xc(%esi),%edx
777531f3: mov %edx,-0x68(%ebp)
777531f6: mov (%edx),%edx
777531f8: mov 0x4(%edi),%edi
777531fb: cmp %edi,%edx
777531fd: jne 0x7779455b <ntdll!RtlGetProcessHeaps+186>
77753203: cmp %ecx,%edx
77753205: jne 0x7779455b <ntdll!RtlGetProcessHeaps+186>
7775320b: sub %ebx,0x78(%eax)
7775320e: mov 0xb8(%eax),%eax
77753214: test %eax,%eax
77753216: je 0x777532ed <ntdll!RtlQueryPerformanceCounter+2457>
7775321c: movzwl (%esi),%edi
7775321f: mov %eax,-0xb4(%ebp)
77753225: mov 0x4(%eax),%ecx
77753228: cmp %ecx,%edi
7775322a: jae 0x777537b3 <ntdll!RtlQueryPerformanceCounter+3679>
77753230: mov %edi,-0x8c(%ebp)
77753236: mov -0x8c(%ebp),%ecx
7775323c: sub 0x14(%eax),%ecx
7775323f: mov %ecx,0x10(%ebp)
77753242: cmpl [=12=]x0,0x8(%eax)
77753246: je 0x7775324a <ntdll!RtlQueryPerformanceCounter+2294>
77753248: add %ecx,%ecx
7775324a: mov %ecx,%edi
7775324c: shl [=12=]x2,%edi
7775324f: mov 0x20(%eax),%ecx
77753252: lea (%edi,%ecx,1),%edx
77753255: mov (%edx),%ecx
77753257: mov %ecx,-0x38(%ebp)
7775325a: decl 0xc(%eax)
7775325d: mov 0x4(%eax),%ecx
77753260: dec %ecx
77753261: cmp %ecx,-0x8c(%ebp)
......... ...
我使用 std::string 而不是 char*
重写了代码
//============================================================================
// Name : sum.cpp
// Author : Yordan Borisov
// Version :
// Copyright : MIT License
//
// Description : Sum bug numbers in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int _INT(char ch) {
return ch - '0';
}
char _CHAR(int n) {
return n + '0';
}
void validateSize(int size_one, int size_two, string& _n_T, string& _n_O) {
if (size_one != size_two) {
int diff = abs(size_one - size_two);
string tmp;
for (int i = 0; i < diff; i++) {
tmp.append("0");
}
tmp.append((size_one > size_two ? _n_T : _n_O));
(size_one > size_two ? _n_T : _n_O).clear();
(size_one > size_two ? _n_T : _n_O).append(tmp);
}
}
string sum(const string numOne, const string numTwo) {
int size_one = numOne.length();
int size_two = numTwo.length();
string _n_O(numOne);
string _n_T(numTwo);
validateSize(size_one, size_two, _n_T, _n_O);
string result = "";
int _CF = 0;
for (long i = (size_one > size_two ? size_one : size_two) - 1; i >= 0;
i--) {
char chOne = _n_O[i];
char chTwo = _n_T[i];
int _sum = _INT(chOne) + _INT(chTwo) + _CF;
_CF = _sum / 10;
result.append(string(1, _CHAR(_sum % 10)));
if (i == 0 && _CF > 0) {
//in that case additional element in the result must be added.
string tmp(1, _CHAR(_CF));
result.append(tmp);
}
}
reverse(result.begin(), result.end());
return result;
}
int main(int argc, char* argv[]) {
string res =
sum(
"147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214",
"3652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144");
cout << res << endl;
return 0;
}
它正在运行,但速度很慢。所以我想问题出在字符指针的分配上。
所以我解决了这个问题:它在
char* result = new char[(size_one > size_two ? size_one : size_two) + 1];
result[(size_one > size_two ? size_one : size_two)] = '[=10=]';
我当时用的是:
char* result = new char[size_one + 1];
result[size_one] = '[=11=]';
这显然是错误的,因为 size_one
比 size_two
小。
当我运行这段代码时:
//============================================================================
// Name : sum.cpp
// Author : Yordan Borisov
// Version :
// Copyright : MIT License
//
// Description : Sum bug numbers in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int _INT(char ch) {
return ch - '0';
}
char _CHAR(int n) {
return n + '0';
}
char* validateSize(int size_one, int size_two, char* _n_T, char*& _n_O) {
if (size_one != size_two) {
int diff = abs(size_one - size_two);
int _less_Length = strlen((size_one > size_two ? _n_T : _n_O));
char* tmp = new char[_less_Length + diff];
strcpy(tmp, "[=10=]");
int _index = 0;
for (int i = 0; i < diff; i++) {
tmp[_index++] = '0';
}
tmp[_index] = '[=10=]';
for (int i = 0; i < _less_Length; i++) {
tmp[_index++] = (size_one > size_two ? _n_T : _n_O)[i];
}
tmp[_index] = '[=10=]';
(size_one > size_two ? _n_T : _n_O) = tmp;
}
return _n_T;
}
char* sum(const char* numOne, const char* numTwo) {
int size_one = strlen(numOne);
int size_two = strlen(numTwo);
char* _n_O = new char[size_one];
char* _n_T = new char[size_two];
strcpy(_n_O, numOne);
strcpy(_n_T, numTwo);
_n_T = validateSize(size_one, size_two, _n_T, _n_O);
char* result = new char[size_one + 1];
result[size_one] = '[=10=]';
int _CF = 0;
int _increase = 0;
for (long i = (size_one > size_two ? size_one : size_two) - 1; i >= 0; i--) {
char chOne = _n_O[i];
char chTwo = _n_T[i];
int _sum = _INT(chOne) + _INT(chTwo) + _CF;
_CF = _sum / 10;
result[i] = _CHAR(_sum % 10);
if (i == 0 && _CF > 0) {
//in that case additional element in the result must be added.
char _ch = _CHAR(_CF);
char* _tmp_Pr = new char[1];
_tmp_Pr[0] = _ch;
_tmp_Pr[1] = '[=10=]';
_increase = 1;
char* _tmp_R = strcat(_tmp_Pr, result);
strcpy(result, _tmp_R);
}
}
result[(size_one > size_two ? size_one : size_two) + _increase] = '[=10=]';
return result;
}
int main(int argc, char* argv[]) {
cout
<< sum(
"147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214",
"3652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144")
<< endl;
return 0;
}
我遇到了这个异常:
Thread #1 0 (Suspended : Signal : SIGSEGV:Segmentation fault)
ntdll!RtlQueryPerformanceCounter() at 0x777531f6
ntdll!RtlQueryPerformanceCounter() at 0x77753431
ntdll!RtlpNtEnumerateSubKey() at 0x777f0f0e
ntdll!RtlUlonglongByteSwap() at 0x777aaa86
0x740000
ntdll!RtlQueryPerformanceCounter() at 0x77753431
towlower() at 0x7700ade8
towlower() at 0x7700ae43
msvcrt!_close() at 0x77013bb9
0x1
<...more frames...>
在第777764bc行的反汇编代码中:
......... ...
777531c2: dec %esp
777531c3: add %dh,-0x75(%esi,%edx,1)
777531c7: dec %eax
777531c8: push %eax
777531c9: xor %ecx,(%esi)
777531cb: mov 0x2(%esi),%cl
777531ce: xor 0x1(%esi),%cl
777531d1: xor (%esi),%cl
777531d3: cmp %cl,0x3(%esi)
777531d6: jne 0x777aab1a <ntdll!RtlUlonglongByteSwap+56842>
777531dc: movzwl (%esi),%ebx
777531df: cmp -0x2c(%ebp),%ebx
777531e2: jb 0x77794568 <ntdll!RtlGetProcessHeaps+199>
777531e8: lea 0x8(%esi),%ecx
777531eb: mov (%ecx),%edi
777531ed: mov %edi,-0x48(%ebp)
777531f0: mov 0xc(%esi),%edx
777531f3: mov %edx,-0x68(%ebp)
777531f6: mov (%edx),%edx
777531f8: mov 0x4(%edi),%edi
777531fb: cmp %edi,%edx
777531fd: jne 0x7779455b <ntdll!RtlGetProcessHeaps+186>
77753203: cmp %ecx,%edx
77753205: jne 0x7779455b <ntdll!RtlGetProcessHeaps+186>
7775320b: sub %ebx,0x78(%eax)
7775320e: mov 0xb8(%eax),%eax
77753214: test %eax,%eax
77753216: je 0x777532ed <ntdll!RtlQueryPerformanceCounter+2457>
7775321c: movzwl (%esi),%edi
7775321f: mov %eax,-0xb4(%ebp)
77753225: mov 0x4(%eax),%ecx
77753228: cmp %ecx,%edi
7775322a: jae 0x777537b3 <ntdll!RtlQueryPerformanceCounter+3679>
77753230: mov %edi,-0x8c(%ebp)
77753236: mov -0x8c(%ebp),%ecx
7775323c: sub 0x14(%eax),%ecx
7775323f: mov %ecx,0x10(%ebp)
77753242: cmpl [=12=]x0,0x8(%eax)
77753246: je 0x7775324a <ntdll!RtlQueryPerformanceCounter+2294>
77753248: add %ecx,%ecx
7775324a: mov %ecx,%edi
7775324c: shl [=12=]x2,%edi
7775324f: mov 0x20(%eax),%ecx
77753252: lea (%edi,%ecx,1),%edx
77753255: mov (%edx),%ecx
77753257: mov %ecx,-0x38(%ebp)
7775325a: decl 0xc(%eax)
7775325d: mov 0x4(%eax),%ecx
77753260: dec %ecx
77753261: cmp %ecx,-0x8c(%ebp)
......... ...
我使用 std::string 而不是 char*
重写了代码//============================================================================
// Name : sum.cpp
// Author : Yordan Borisov
// Version :
// Copyright : MIT License
//
// Description : Sum bug numbers in C++, Ansi-style
//============================================================================
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int _INT(char ch) {
return ch - '0';
}
char _CHAR(int n) {
return n + '0';
}
void validateSize(int size_one, int size_two, string& _n_T, string& _n_O) {
if (size_one != size_two) {
int diff = abs(size_one - size_two);
string tmp;
for (int i = 0; i < diff; i++) {
tmp.append("0");
}
tmp.append((size_one > size_two ? _n_T : _n_O));
(size_one > size_two ? _n_T : _n_O).clear();
(size_one > size_two ? _n_T : _n_O).append(tmp);
}
}
string sum(const string numOne, const string numTwo) {
int size_one = numOne.length();
int size_two = numTwo.length();
string _n_O(numOne);
string _n_T(numTwo);
validateSize(size_one, size_two, _n_T, _n_O);
string result = "";
int _CF = 0;
for (long i = (size_one > size_two ? size_one : size_two) - 1; i >= 0;
i--) {
char chOne = _n_O[i];
char chTwo = _n_T[i];
int _sum = _INT(chOne) + _INT(chTwo) + _CF;
_CF = _sum / 10;
result.append(string(1, _CHAR(_sum % 10)));
if (i == 0 && _CF > 0) {
//in that case additional element in the result must be added.
string tmp(1, _CHAR(_CF));
result.append(tmp);
}
}
reverse(result.begin(), result.end());
return result;
}
int main(int argc, char* argv[]) {
string res =
sum(
"147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214",
"3652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144");
cout << res << endl;
return 0;
}
它正在运行,但速度很慢。所以我想问题出在字符指针的分配上。
所以我解决了这个问题:它在
char* result = new char[(size_one > size_two ? size_one : size_two) + 1];
result[(size_one > size_two ? size_one : size_two)] = '[=10=]';
我当时用的是:
char* result = new char[size_one + 1];
result[size_one] = '[=11=]';
这显然是错误的,因为 size_one
比 size_two
小。