当我尝试在 Eclipse CDT 中运行我的程序时,我的可执行文件停止 运行
My executable stops running when I try to runmy program in Eclipse CDT
在我构建我的项目后,我似乎收到了这条消息,它说我的程序 Cyber Dojo 1
已停止工作。如下所示:
现在网上有一些资源,包括:
This SO post,其中有一个答案未被接受。答案无效,因为我的程序没有任何参数。
This forum post itself on the Eclipse Community Forums. This has a few good suggestions, especially the one that relates to changing MinGW's linker flags. However, this would apply to a C++ program and not a C program. This 也是处理相同问题的 post,但同样适用于 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;
}
如果你是 Eclipse 的初学者,不知道如何使用调试器,你可以看一些教程。
https://www.youtube.com/watch?v=azInZkPP56Q
但即使在本教程之后,您也很难让调试器正常工作(因为有时这取决于 Eclipse、编译器和其他东西的安装方式),您可以尝试将大部分代码放在评论中,看看如果问题消失了。一点一点地减少你放在注释中的代码量。当错误再次出现时,这意味着它位于您最近删除评论的部分内。
最后一种方法不是调试程序的最佳方法,但对初学者很有用,如果您使用调试器有困难,可以为您提供另一种选择。
在我构建我的项目后,我似乎收到了这条消息,它说我的程序 Cyber Dojo 1
已停止工作。如下所示:
现在网上有一些资源,包括:
This SO post,其中有一个答案未被接受。答案无效,因为我的程序没有任何参数。
This forum post itself on the Eclipse Community Forums. This has a few good suggestions, especially the one that relates to changing MinGW's linker flags. However, this would apply to a C++ program and not a C program. This 也是处理相同问题的 post,但同样适用于 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;
}
如果你是 Eclipse 的初学者,不知道如何使用调试器,你可以看一些教程。
https://www.youtube.com/watch?v=azInZkPP56Q
但即使在本教程之后,您也很难让调试器正常工作(因为有时这取决于 Eclipse、编译器和其他东西的安装方式),您可以尝试将大部分代码放在评论中,看看如果问题消失了。一点一点地减少你放在注释中的代码量。当错误再次出现时,这意味着它位于您最近删除评论的部分内。
最后一种方法不是调试程序的最佳方法,但对初学者很有用,如果您使用调试器有困难,可以为您提供另一种选择。