函数在正常运行时不起作用,但在调试时起作用



我有以下代码:

#include <iostream>
#include <cmath>
bool primes[21] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0};
int find_last_power(int n, int p){
return (int) std::pow(n, (double) 1/p);
}
long long solve(int n){
long long solution = 1;
for (int i=2; i<=n; i++){
if (primes[i]){
std::cout << "p" << i << " : " << std::pow(i, find_last_power(n, i)) << std::endl;
solution *= static_cast<long long>(std::pow(i, find_last_power(n, i)));
}
}
return solution;
}
int main(){
std::cout << solve(20); return 0;
}
  • primes是一个n+1布尔值的数组,如果i是素数,则其值primes[i]true,如果i是复合值,则值为 false。
  • find_last_power(n, p)返回小于或等于n的最大p幂的指数(int(。

如果你运行程序,它会写出:

p2 : 16
p3 : 9
p5 : 5
p7 : 7
p11 : 11
p13 : 13
p17 : 17
p19 : 19
214885440 // this is the return value of solve(20)
// it is supposed to be the product of the numbers on the right (16,9...)

但返回的数字不是预期的输出。但是,该程序在调试器中正确运行,这就是为什么我发现很难识别错误的原因。预期输出应232792560

任何帮助,不胜感激。


根据要求,这里是汇编程序源。

.file   "PE_5.cxx"
.text
.section .rdata,"dr"
__ZStL19piecewise_construct:
.space 1
.lcomm __ZStL8__ioinit,1,1
.globl  _primes
.data
.align 4
_primes:
.byte   0
.byte   0
.byte   1
.byte   1
.byte   0
.byte   1
.byte   0
.byte   1
.byte   0
.byte   0
.byte   0
.byte   1
.byte   0
.byte   1
.byte   0
.byte   0
.byte   0
.byte   1
.byte   0
.byte   1
.byte   0
.text
.globl  __Z15find_last_powerii
.def    __Z15find_last_powerii; .scl    2;  .type   32; .endef
__Z15find_last_powerii:
LFB1717:
.cfi_startproc
pushl   %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl    %esp, %ebp
.cfi_def_cfa_register 5
subl    $40, %esp
fildl   12(%ebp)
fld1
fdivp   %st, %st(1)
fstpl   4(%esp)
movl    8(%ebp), %eax
movl    %eax, (%esp)
call    __ZSt3powIidEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_
fnstcw  -10(%ebp)
movzwl  -10(%ebp), %eax
orb $12, %ah
movw    %ax, -12(%ebp)
fldcw   -12(%ebp)
fistpl  -16(%ebp)
fldcw   -10(%ebp)
movl    -16(%ebp), %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1717:
.section .rdata,"dr"
LC2:
.ascii "p"
LC3:
.ascii " : "
.text
.globl  __Z5solvei
.def    __Z5solvei; .scl    2;  .type   32; .endef
__Z5solvei:
LFB1718:
.cfi_startproc
pushl   %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl    %esp, %ebp
.cfi_def_cfa_register 5
pushl   %ebx
subl    $52, %esp
.cfi_offset 3, -12
movl    $1, -16(%ebp)
movl    $0, -12(%ebp)
movl    $2, -20(%ebp)
L6:
movl    -20(%ebp), %eax
cmpl    8(%ebp), %eax
jg  L4
movl    -20(%ebp), %eax
addl    $_primes, %eax
movzbl  (%eax), %eax
testb   %al, %al
je  L5
movl    $LC2, 4(%esp)
movl    $__ZSt4cout, (%esp)
call    __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl    %eax, %edx
movl    -20(%ebp), %eax
movl    %eax, (%esp)
movl    %edx, %ecx
call    __ZNSolsEi
subl    $4, %esp
movl    $LC3, 4(%esp)
movl    %eax, (%esp)
call    __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl    %eax, %ebx
movl    -20(%ebp), %eax
movl    %eax, 4(%esp)
movl    8(%ebp), %eax
movl    %eax, (%esp)
call    __Z15find_last_powerii
movl    %eax, 4(%esp)
movl    -20(%ebp), %eax
movl    %eax, (%esp)
call    __ZSt3powIiiEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_
fstpl   (%esp)
movl    %ebx, %ecx
call    __ZNSolsEd
subl    $8, %esp
movl    $__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, (%esp)
movl    %eax, %ecx
call    __ZNSolsEPFRSoS_E
subl    $4, %esp
movl    -20(%ebp), %eax
movl    %eax, 4(%esp)
movl    8(%ebp), %eax
movl    %eax, (%esp)
call    __Z15find_last_powerii
movl    %eax, 4(%esp)
movl    -20(%ebp), %eax
movl    %eax, (%esp)
call    __ZSt3powIiiEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_
fnstcw  -26(%ebp)
movzwl  -26(%ebp), %eax
orb $12, %ah
movw    %ax, -28(%ebp)
fldcw   -28(%ebp)
fistpq  -40(%ebp)
fldcw   -26(%ebp)
movl    -40(%ebp), %eax
movl    -36(%ebp), %edx
movl    -12(%ebp), %ecx
movl    %ecx, %ebx
imull   %eax, %ebx
movl    -16(%ebp), %ecx
imull   %edx, %ecx
addl    %ebx, %ecx
mull    -16(%ebp)
addl    %edx, %ecx
movl    %ecx, %edx
movl    %eax, -16(%ebp)
movl    %edx, -12(%ebp)
movl    %eax, -16(%ebp)
movl    %edx, -12(%ebp)
L5:
addl    $1, -20(%ebp)
jmp L6
L4:
movl    -16(%ebp), %eax
movl    -12(%ebp), %edx
movl    -4(%ebp), %ebx
leave
.cfi_restore 5
.cfi_restore 3
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1718:
.def    ___main;    .scl    2;  .type   32; .endef
.globl  _main
.def    _main;  .scl    2;  .type   32; .endef
_main:
LFB1719:
.cfi_startproc
leal    4(%esp), %ecx
.cfi_def_cfa 1, 0
andl    $-16, %esp
pushl   -4(%ecx)
pushl   %ebp
.cfi_escape 0x10,0x5,0x2,0x75,0
movl    %esp, %ebp
pushl   %ecx
.cfi_escape 0xf,0x3,0x75,0x7c,0x6
subl    $20, %esp
call    ___main
movl    $20, (%esp)
call    __Z5solvei
movl    %eax, (%esp)
movl    %edx, 4(%esp)
movl    $__ZSt4cout, %ecx
call    __ZNSolsEx
subl    $8, %esp
movl    $0, %eax
movl    -4(%ebp), %ecx
.cfi_def_cfa 1, 0
leave
.cfi_restore 5
leal    -4(%ecx), %esp
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1719:
.section    .text$_ZSt3powIidEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_,"x"
.linkonce discard
.globl  __ZSt3powIidEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_
.def    __ZSt3powIidEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_;    .scl    2;  .type   32; .endef
__ZSt3powIidEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_:
LFB1955:
.cfi_startproc
pushl   %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl    %esp, %ebp
.cfi_def_cfa_register 5
subl    $40, %esp
movl    12(%ebp), %eax
movl    %eax, -16(%ebp)
movl    16(%ebp), %eax
movl    %eax, -12(%ebp)
fildl   8(%ebp)
fldl    -16(%ebp)
fstpl   8(%esp)
fstpl   (%esp)
call    _pow
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1955:
.section    .text$_ZSt3powIiiEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_,"x"
.linkonce discard
.globl  __ZSt3powIiiEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_
.def    __ZSt3powIiiEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_;    .scl    2;  .type   32; .endef
__ZSt3powIiiEN9__gnu_cxx11__promote_2IT_T0_NS0_9__promoteIS2_XsrSt12__is_integerIS2_E7__valueEE6__typeENS4_IS3_XsrS5_IS3_E7__valueEE6__typeEE6__typeES2_S3_:
LFB1957:
.cfi_startproc
pushl   %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl    %esp, %ebp
.cfi_def_cfa_register 5
subl    $24, %esp
fildl   12(%ebp)
fildl   8(%ebp)
fxch    %st(1)
fstpl   8(%esp)
fstpl   (%esp)
call    _pow
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1957:
.text
.def    ___tcf_0;   .scl    3;  .type   32; .endef
___tcf_0:
LFB2201:
.cfi_startproc
pushl   %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl    %esp, %ebp
.cfi_def_cfa_register 5
subl    $8, %esp
movl    $__ZStL8__ioinit, %ecx
call    __ZNSt8ios_base4InitD1Ev
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE2201:
.def    __Z41__static_initialization_and_destruction_0ii;   .scl    3;  .type   32; .endef
__Z41__static_initialization_and_destruction_0ii:
LFB2200:
.cfi_startproc
pushl   %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl    %esp, %ebp
.cfi_def_cfa_register 5
subl    $24, %esp
cmpl    $1, 8(%ebp)
jne L17
cmpl    $65535, 12(%ebp)
jne L17
movl    $__ZStL8__ioinit, %ecx
call    __ZNSt8ios_base4InitC1Ev
movl    $___tcf_0, (%esp)
call    _atexit
L17:
nop
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE2200:
.def    __GLOBAL__sub_I_primes; .scl    3;  .type   32; .endef
__GLOBAL__sub_I_primes:
LFB2202:
.cfi_startproc
pushl   %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl    %esp, %ebp
.cfi_def_cfa_register 5
subl    $24, %esp
movl    $65535, 4(%esp)
movl    $1, (%esp)
call    __Z41__static_initialization_and_destruction_0ii
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE2202:
.section    .ctors,"w"
.align 4
.long   __GLOBAL__sub_I_primes
.ident  "GCC: (MinGW.org GCC-8.2.0-3) 8.2.0"
.def    __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;   .scl    2;  .type   32; .endef
.def    __ZNSolsEi; .scl    2;  .type   32; .endef
.def    __ZNSolsEd; .scl    2;  .type   32; .endef
.def    __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_;    .scl    2;  .type   32; .endef
.def    __ZNSolsEPFRSoS_E;  .scl    2;  .type   32; .endef
.def    __ZNSolsEx; .scl    2;  .type   32; .endef
.def    _pow;   .scl    2;  .type   32; .endef
.def    __ZNSt8ios_base4InitD1Ev;   .scl    2;  .type   32; .endef
.def    __ZNSt8ios_base4InitC1Ev;   .scl    2;  .type   32; .endef
.def    _atexit;    .scl    2;  .type   32; .endef

它是使用以下命令编译的(在64位Intel i5 4690k,Windows 10上(:

g++ -S -o asm.s PE_5.cxx
g++ -c asm.s -o outtput.o
g++ output.o -o out.exe
g++ --version
// g++ (MinGW.org GCC-8.2.0-3) 8.2.0

你有 int 溢出。更改返回的类型

int solve(int n){

long long solve(int n){

最新更新