【问题标题】:My executable stops running when I try to runmy program in Eclipse CDT当我尝试在 Eclipse CDT 中运行我的程序时,我的可执行文件停止运行
【发布时间】:2015-10-13 01:28:36
【问题描述】:

在我构建我的项目后,我似乎收到了这条消息,它说我的程序 Cyber Dojo 1 已停止工作。如下图所示:

现在网上有一些资源,包括:

  1. This SO 帖子,其中有一个答案未被接受。答案无效,因为我的程序没有任何参数。

  2. This 论坛在 Eclipse 社区论坛上发帖。这有一些很好的建议,尤其是与更改 MinGW 的链接器标志有关的建议。但是,这将适用于 C++ 程序而不是 C 程序。 This 也是处理相同问题的帖子,但又是针对 C++ 的。

这就是为什么我目前正在为我的 Eclipse CDT 上的 C 程序寻找这个问题的解决方案。


这是我的代码:

     //Checking number as input
     static void isNotValidCharacter1(void)
     {
        assert(answer('3') == NULL);
     }

//Checking special character
static void isNotValidCharacter2(void)
{
    assert(answer('!') == NULL);
}

//Checking lowercase letter
static void isNotValidCharacter3(void)
{
    assert(answer('c') == NULL);
}


static void validCharacter(char **sample_answer)
{
    int i, j;
    for (i = 1; i < 11; i++) {
        for (j = 1; j < 11; j++) {
            assert((answer('F'))[i][j] == sample_answer[i][j]);
        }
    }
}

//Random Number Corner Checks Follow:

// Randomly creates a number/ character and checks the leftmost and rightmost corner characters
// as the character itself

static char psuedoRandomNumberGeneratedCharacterCheck1(void)
{
    // Creating the random number between 65 and 90
    int rn;
    srand(time(NULL));
    rn = (rand() % 25) + 65;
    int distA = rn - 65;

    //converting it to a character
    char c_rn = (char)rn;
    //checking the leftmost and rightmost corner characters
    assert(answer(rn)[distA][0] == c_rn);
    assert(answer(rn)[distA][distA*2] == c_rn);
    return c_rn;
}

// Randomly creates a number/ characters and the checks the uppermost and lowermost corner characters
// corners as 'A'

static char psuedoRandomNumberGeneratedCharacterCheck2(void)
{
    // Creating the random number between 65 and 90
    int rn;
    srand(time(NULL));
    rn = (rand() % 25) + 65;
    int distA = rn - 65;

    //converting it to a character
    char c_rn = (char)rn;
    //checking the uppermost and lowermost corner characters
    assert(answer(rn)[0][distA] == 'A');
    assert(answer(rn)[distA*2][distA] == 'A');
    return c_rn;
}

static void validCharacterA(void)
{
    char **aDiamond = answer('A');
    aDiamond[0][0] = 'A';
}

int main(void)
{
    //Not valid character tests
    isNotValidCharacter1();
    puts("Number not accepted");
    puts("special pause for debugging");
    isNotValidCharacter2();
    puts("Special Character not accepted");
    isNotValidCharacter3();
    puts("lowercase not accepted");

    //Psuedorandom Tests

    char prc1 = psuedoRandomNumberGeneratedCharacterCheck1();
    printf("random character '%c' chosen and the leftmost and rightmost corner characters", prc1);
    char prc2 = psuedoRandomNumberGeneratedCharacterCheck2();
    printf("random character '%c' chosen and the leftmost and rightmost corner characters", prc2);

    // Acid Test for the letter 'F'

    //Square of 11 letters declared
    char **Fanswer = malloc(11 * sizeof(*Fanswer));
    int i;
    for (i =0; i  <11; i++) {
        Fanswer[i] = malloc(11 * sizeof(char));
    }

    strcpy( Fanswer[0], "     A     ");
    strcpy( Fanswer[1], "    B B    ");
    strcpy( Fanswer[2], "   C   C   ");
    strcpy( Fanswer[3], "  D     D  ");
    strcpy( Fanswer[4], " E       E ");
    strcpy( Fanswer[5], "F         F");
    strcpy( Fanswer[6], " E       E ");
    strcpy( Fanswer[7], "  D     D  ");
    strcpy( Fanswer[8], "   C   C   ");
    strcpy( Fanswer[9], "    B B    ");
    strcpy(Fanswer[10], "     A     ");

    validCharacter(Fanswer);
    puts("answer for F is correct");

    validCharacterA();
puts("Answer for A is correct");

    //All tests have passed and the end of the program
    puts("All tests passed");
}

我的answer() 程序如下:

char** answer(char c)
{

    if (check(c)) {
        printf("\n");
    } else {
        printf("Not a valid character\n");
        return NULL;
    }


    //--------------------------------------------------------------------------------------------------------
    //   Preprocessing
    //--------------------------------------------------------------------------------------------------------

    //processing declarations
    int ascii = (int)c;
    int distA = ascii - 'A';

    //Number of Rows and Columns
    int n = ( distA * 2 ) + 1;

    //Declare the column of pointers
    char **diamond = malloc(n * sizeof(*diamond));

    //Declare the row of characters
    // 2D array declared here to save on computation in situations where characters are not valid
    int i;
    for (i=0; i<n; i++) {
            diamond[i] = malloc(n * sizeof(char));
    }

    //--------------------------------------------------------------------------------------------------
    //   Processing
    //--------------------------------------------------------------------------------------------------

    //Fill in the Array
    if (n == 1) {
        diamond[0][0] = c;
    } else {
        diamond[distA][0] = c;
        diamond[distA][distA*2] = c;
        for (i = 1; i <= distA; i++) {
            diamond[distA-i][i] = (char)(ascii - i);
            diamond[distA-i][(distA*2)-i] = (char)(ascii - i);
            diamond[distA+i][i] = (char)(ascii - i);
            diamond[distA+i][(distA*2)-i] = (char)(ascii - i);
        }
    }

    //-------------------------------------------------------------------------------------------------
    //   Postprocessing
    //---------------------------------------------------------------------------
    return diamond;
}

【问题讨论】:

  • 请把相关的节目贴出来,你发的帖子都没用。您的程序显然崩溃了,但除非周围有人是通灵者,否则无法猜测原因。
  • @iharob 我的建筑完全没有错误,我不知道我应该在这里发布我的程序的哪一部分。
  • 它与构建过程无关。您可以轻松地在c 中编写代码并编译它而不会出现错误或警告,但仍然会崩溃。示例char *overflow; overflow = malloc(100); overflow[100] = '\0'; 就是这样,它可能会崩溃。它会导致未定义的行为,它可能正在读取内存区域,从而使系统发出 SIGSEGV 信号,指示无效的内存访问 Segmentation Fault
  • @iharob 让我检查一下一个简单的 helloworld 程序是否可以工作,如果可以,我将发布我的程序中一些更重要的mallocs
  • 发布一个重现行为的程序

标签: c eclipse linker execution eclipse-cdt


【解决方案1】:

如果你是 Eclipse 的初学者并且你不知道如何使用调试器,你可以看一些教程。

https://www.youtube.com/watch?v=azInZkPP56Q

但是即使在本教程之后,您仍然很难使调试器正常工作(因为有时这取决于您的 Eclipse、编译器和其他东西的安装方式),您可以尝试将大部分代码放在注释中,看看是否问题消失了。并且一点一点地减少您在注释中添加的代码量。当错误再次出现时,这意味着它位于您最近删除评论的部分内。

最后一种方法不是调试程序的最佳方法,但对初学者很有用,如果您使用调试器有困难,可以为您提供另一种选择。

【讨论】:

  • 会的!一旦我尝试了调试器,我会回到这个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2021-04-15
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 2021-08-22
  • 2020-10-12
相关资源
最近更新 更多