Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数不';正常运行时不工作,但调试时不工作_C++_C++11 - Fatal编程技术网

C++ 函数不';正常运行时不工作,但调试时不工作

C++ 函数不';正常运行时不工作,但调试时不工作,c++,c++11,C++,C++11,我有以下代码: #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){

我有以下代码:

#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;
}
但是返回的数字不是预期的输出。然而,该程序在调试器中正确运行,这就是我发现很难识别错误的原因。预期输出应为
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\0"
LC3:
    .ascii " : \0"
    .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上):


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

int solve(int n){


16*9*5*7*11*13*17是12252240。12252240*19是232792560。你是怎么得到232792237的?程序返回了那个号码。你说得对,这不是问题中提到的产品。我将编辑它。我无法在WSL上使用我的64位Clang++在-O3或-O0以及UBAN或无消毒剂的任何组合下复制它。
std::pow
返回一个
double
,因此
solution*=std::pow(…)
将以
double
的形式执行乘法,然后返回到
long
。我唯一的猜测是,这可能会引入舍入误差。你能先试一试吗<代码>解决方案*=static_cast(std::pow(…)。代码在这里运行良好。OP,您确定编译了正确的文件并启动了正确的可执行文件吗?实际溢出的整数值是什么?预期输出(232 792 560)小于20亿,适合32位、有符号、2秒补码整数,这是许多平台上
int
的典型情况。+1,因为这是一个bug。不过,在这种情况下没有溢出。更改返回类型后,错误仍然存在。@AndreyAkhmetov您是对的。快速看一眼,我认为它超过了20亿。操作编号让我困惑:232792237*19产生32位溢出。
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 solve(int n){
long long solve(int n){