简体   繁体   English

代码通过调试器运行良好,但在执行时崩溃

[英]Code runs fine through a debugger but crashes on execution

Can someone point out or give a hint on what's going on? 有人可以指出或暗示发生了什么事吗? Why is it when I run the code line-by-line using the built-in debugger, it gives the correct returnAry , but crashes when I try to execute the program? 为什么当我使用内置调试器逐行运行代码时,为什么它提供正确的returnAry ,但是在我尝试执行程序时崩溃?

No debugger: 没有调试器: 在此处输入图片说明

With debugger: 使用调试器: 在此处输入图片说明 在此处输入图片说明

Here is my code: 这是我的代码:

#include <iostream>
#include "fraction.h"
#include "fractionUtilities.h"
using namespace std;

int* getUncommon(Fraction*, int);

int main() {
    Fraction testAry[] = { 1201, 6266, 35, 77 };
    int size = 4;
    int* result;
    result = getUncommon(testAry, size);

    for (int i = 0; i < result[0] + 1; i++) {
        cout << result[i] << endl;
    }
    return 0;
}

int* getUncommon(Fraction* ary, int size) {
    int* returnAry = 0;
    int tmp;
    int** digitInfoAry = new int*[size];
    int i, j;
    int sizeAry = 10;
    int digitAry[10]{ 0 };
    int uncommonDigitCount = 0;

    for (i = 0; i < sizeAry; i++) {
        *(digitInfoAry + i) = new int[sizeAry] {0};
    }

    for (i = 0; i < size; i++) {
        tmp = (ary + i)->getNum() < 0 ? -(ary + i)->getNum() : (ary + i)->getNum();

        do {
            *(*(digitInfoAry + i) + tmp % 10) = 1;
            tmp /= 10;
        } while (tmp != 0);
    }

    for (i = 0; i < sizeAry; i++) {
        for (j = 0; j < size; j++) {
            digitAry[i] += *(*(digitInfoAry + j) + i);
        }
    }

    for (i = 0; i < sizeAry; i++) {
        if (digitAry[i] == 1) {
            uncommonDigitCount++;
        }
    }

    returnAry = new int[uncommonDigitCount + 1];
    *returnAry = uncommonDigitCount;

    if (uncommonDigitCount != 0) {
        for (i = 0, j = 1; i < sizeAry; i += 2) {
            if (digitAry[i] % 2 == 1) {
                returnAry[j] = i;
                j++;
            }
        }

        for (i = 1; i < sizeAry; i += 2) {
            if (digitAry[i] % 2 == 1) {
                returnAry[j] = i;
                j++;
            }
        }
    }

    return returnAry;
}

Thank you ahead of time for your help, I really cannot figure out what is going on, it's driving me insane! 提前谢谢您的帮助,我真的不知道发生了什么,这使我发疯了!

Try to fix this: 尝试解决此问题:

int** digitInfoAry = new int*[size];
...
for (i = 0; i < sizeAry; i++) {
    *(digitInfoAry + i) = new int[sizeAry] {0};
}

Then loop runs from 0 to sizeAry indices goes beyond allocated memory. 然后循环从0sizeAry索引超出分配的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调试时我的代码运行正常,但是一旦运行它就会崩溃 - My code runs fine when I debug it, but crashes as soon as I run it 如果调试运行正常但释放崩溃怎么办 - what to do if debug runs fine, but release crashes 如何创建简单的 C++ 代码,在没有数据执行保护 (DEP) 的情况下运行良好,但在 DEP 开启时会崩溃? - How can I create simple C++ code that runs fine with no Data Execution Prevention (DEP) but will crash with DEP on? 多项式类在调试器中运行良好,但在尝试构建和运行时却无法运行 - Polynomial Class runs fine in debugger but not when trying to build&run Visual Studio调试器失败,但程序运行时运行良好......? - Visual Studio debugger fails but program runs fine when built…? SFML 2.1程序在调试模式下运行良好,但在发布模式下崩溃 - SFML 2.1 program runs fine in debug mode, but crashes in release mode 代码在代码块上运行良好,但无法在SPOJ上运行 - The code runs fine on codeblock but fails to run on SPOJ 带有断点的代码运行良好,但没有断点就不行 - Code runs fine with breakpoints but NOT without them 使用WinDbg可以很好地运行代码,但是如果没有它就很奇怪 - Code runs fine with WinDbg but weird without it 代码在xcode中可以正常运行,但在命令行中会出错 - Code runs fine in xcode, but errors out on the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM