【发布时间】:2015-10-13 01:28:36
【问题描述】:
在我构建我的项目后,我似乎收到了这条消息,它说我的程序 Cyber Dojo 1 已停止工作。如下图所示:
现在网上有一些资源,包括:
This SO 帖子,其中有一个答案未被接受。答案无效,因为我的程序没有任何参数。
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