首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C代码可在Eclipse-Kepler中运行,但无法在Codeblocks中运行

C代码可在Eclipse-Kepler中运行,但无法在Codeblocks中运行
EN

Stack Overflow用户
提问于 2013-11-17 03:32:01
回答 1查看 351关注 0票数 0

我是C编程的新手,也是Stackoverflow的新手。

我有一些c代码可以在Eclipse Kepler (Java EE IDE )中编译和运行;我安装了C/C++插件和Cygwin Gcc编译器for c。一切都在Eclipse IDE中运行得很好;但是,当我的朋友尝试在他的Codeblocks上运行相同的代码时,他没有得到任何输出。在某种程度上,他得到了一些分割错误,我们后来了解到这是由于我们的程序访问了不属于我们程序的内存空间。

Codeblocks集成开发环境使用的是Gcc编译器,而不是cygwin的gcc,但我不认为他们会有那么大的不同来造成这种问题。

我知道C语言是非常原始和非标准化的,但是为什么我的代码可以在cygwin-gcc编译器的eclipse中运行,而不能在Codeblocks的gcc编译器中运行呢?

请帮帮忙,这对我们的班级项目很重要。

感谢所有人。

编辑我们的代码在这里粘贴有点大,但这里有一个在eclipse中成功运行但在代码块中失败的示例代码,如果你有代码块,请自己尝试一下:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>


int main(void) {
    char *traceEntry1;
    FILE *ifp;

    traceEntry1 = malloc(200*sizeof(char));
    ifp = fopen("./program.txt", "r");

    while (fgets(traceEntry1, 75, ifp)) 
      printf("String input is %s \n", traceEntry1);
    fclose(ifp);
}

它根本不会在代码块中给出任何输出,有时只会导致分段错误。

我不知道问题出在哪里。

我们需要您的帮助,请提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2013-11-17 04:48:55

始终并永远测试所有(相关的)调用的结果。“相关”至少是那些在调用失败时返回结果为unusable的调用。

在OP代码的情况下,它们是:

  • malloc()
  • fopen()
  • fclose()

OP代码的保存版本可能如下所示:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
  int result = EXIT_SUCCESS; /* Be optimistic. */
  char * traceEntry1 = NULL; /* Always initialise your variables, you might remove this during the optimisation phase later (if ever) */
  FILE * ifp = NULL;   /* Always initialise your variables, you might remove this during the optimisation phase later (if ever) */

  traceEntry1 = malloc(200 * sizeof(*traceEntry1)); /* sizeof(char) is always 1. 
              Using the dereferenced target (traceEntry1) on the other hand makes this 
              line of code robust against modifications of the target's type declaration. */ 
  if (NULL == traceEntry1)
  {
    perror("malloc() failed"); /* Log error. Never ignore useful and even free informaton. */
    result = EXIT_FAILURE;  /* Flag error and ... */
    goto lblExit;  /* ... leave via the one and only exit point. */
  }

  ifp = fopen("./program.txt", "r");
  if (NULL == ifp)
  {
    perror("fopen() failed"); /* Log error. Never ignore useful and even free informaton. */
    result = EXIT_FAILURE; /* Flag error ... */
    goto lblExit;  /* ... and leave via the one and only exit point. */
  }

  while (fgets(traceEntry1, 75, ifp)) /* Why 75? Why not 200 * sizeof(*traceEntry1) 
                                         as that's what was allocated to traceEntr1? */
  {
    printf("String input is %s \n", traceEntry1);
  }

  if (EOF == fclose(ifp))
  {
    perror("fclose() failed");
    /* Be tolerant as no poisened results are returned. So do not flag error. It's logged however. */
  }

lblExit: /* Only have one exit point. So there is no need to code the clean-up twice. */

  free(traceEntry1); /* Always clean up, free what you allocated. */

  return result; /* Return the outcome of this exercise. */
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20022816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档