确定循环中发生分段错误?

发布于 2024-07-29 02:58:47 字数 1471 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尐籹人 2024-08-05 02:58:47

您确定调用此代码并且“模型”是 1、2 或 3 吗? 如果没有, fptr3 将不会被设置,所以如果你尝试用它做任何事情,你就会遇到麻烦。

试试这个?:

void modeltest (int model) 
{
    FILE *fptr3 = NULL;
    int i = 0;
    int j = 0;
    double freq[20], prob[20][20];

    if(model==1) fptr3=fopen("poisson.pro","r");
    if(model==2) fptr3=fopen("jtt.pro","r");
    if(model==3) fptr3=fopen("estimate.pro","r");

    printf ("here 5a\n");
    if (fptr3 != NULL) {
        for (i=0;i<20;i++) fscanf(fptr3,"%lf", &freq[i]);
        printf ("here 5ai\n");
        for (i=0;i<20;i++) {
            printf ("here 5b\n");
            for (j=0;j<20;j++) {
                printf ("here 5c\n");
                fscanf(fptr3,"%lf", &prob[i][j]);
                if(model==3) prob[i][j]=prob[i][j]/10000.0;
            }
        }
    }
    else {
        printf ("fptr3 is NULL!\n");
    }
}

附加说明。 请,请,请考虑一致的支架样式! :)

Are you sure this code is called and "model" is either 1, 2, or 3? If not, fptr3 would not be set, so if you try to do anything with it you'd be in trouble.

Try this instead?:

void modeltest (int model) 
{
    FILE *fptr3 = NULL;
    int i = 0;
    int j = 0;
    double freq[20], prob[20][20];

    if(model==1) fptr3=fopen("poisson.pro","r");
    if(model==2) fptr3=fopen("jtt.pro","r");
    if(model==3) fptr3=fopen("estimate.pro","r");

    printf ("here 5a\n");
    if (fptr3 != NULL) {
        for (i=0;i<20;i++) fscanf(fptr3,"%lf", &freq[i]);
        printf ("here 5ai\n");
        for (i=0;i<20;i++) {
            printf ("here 5b\n");
            for (j=0;j<20;j++) {
                printf ("here 5c\n");
                fscanf(fptr3,"%lf", &prob[i][j]);
                if(model==3) prob[i][j]=prob[i][j]/10000.0;
            }
        }
    }
    else {
        printf ("fptr3 is NULL!\n");
    }
}

An additonal note. Please, please, please consider consistent brace styles! :)

メ斷腸人バ 2024-08-05 02:58:47

这是C#吗???? 哇,别开玩笑了,你遇到了段错误......
你知道返回码的用途吗???
在寻求此类帮助之前,您确实需要改进代码......这确实是不可读且丑陋的代码......
开始对文件指针添加一些检查

    if(1==model)
{
 fptr3=fopen("poisson.pro","r");
 if(null == fptr3)
{
  perror("Fopen failed");
}

......

Is this C# ???? wooow no kidding you are getting segfaults...
Do you know what return codes are for ???
You really need to improve your code before asking for this kind of help....this is really unreadable, and ugly code...
Start adding some check to your file pointers

    if(1==model)
{
 fptr3=fopen("poisson.pro","r");
 if(null == fptr3)
{
  perror("Fopen failed");
}

...

々眼睛长脚气 2024-08-05 02:58:47

valgrind 是 C 语言中段错误的最佳补救措施。它可以发现错误在段错误之前,并准确地告诉您做错了什么。 为了获得最大收益,请在打开调试符号的情况下进行编译。

valgrind is the sovereign remedy for segfaults in C. It finds errors before the segfault and tells you exactly what you did wrong. For maximum benefit, compile with debugging symbols turned on.

神也荒唐 2024-08-05 02:58:47

我同意丹尼尔的观点,你需要检查你的返回值和指针。

我也同意您需要更好地阻止和格式化代码。 假设你是唯一读过这本书的人,那么几周后,你甚至会忘记得足够多,以至于无法跟上它。

对于马修斯的观点,您可以尝试删除以以下行开头的所有内容:

printf ("here 5ai\n");

当然,只有在检查文件指针后才需要处理此问题。

另一个疯狂的想法是使用调试器单步执行它。 我知道当您有像 printf 语句这样出色的调试选项时,很难使用调试器,但有时调试器可以节省大量时间(例如,当您不断出现段错误时)。 此外,学习如何使用您正在开发的平台的调试器是将优秀程序员与普通黑客区分开来的因素之一。 (仅供参考,您甚至可以尝试在调试器中查看核心,也许可以准确地看出问题出在哪里。(提示:如果您没有核心文件,请尝试“man ulimit”。))

如果您要坚持使用 printf 作为唯一的调试方法,然后在每次使用后立即调用刷新。

I agree with Daniel, you need to be checking your return values and pointers.

I also agree that you need to do a better job blocking and formatting your code. Supposing that you are the only one that ever reads it, in a couple of weeks even you will have forgotten enough that you won't be able to follow it.

Towards Matthews point, you could try deleting everything starting with the line:

printf ("here 5ai\n");

Of course, only bother with this after checking the file pointer.

Another crazy idea would be stepping through it with a debugger. I know it's hard to use a debugger when you have such great debugging options as the printf statement, but sometimes a debugger can be a huge time saver (for example, when you keep getting segfault). Also, learning how to use the debugger for the platform you are developing on is one of the things that separates good coders from the pack of average hacks. (FYI, you could even try looking at your core in the debugger and maybe see exactly where the problem is. (Hint: if you don't have a core file try 'man ulimit'.))

If you are going to insist on using printf as your only means of debug, then call flush right after each use.

云朵有点甜 2024-08-05 02:58:47

freq 定义为什么? 概率定义为什么?

第一个 for 循环是否只针对频率运行 fscanf ? 或者它应该涵盖整个街区?

编辑:基本原理 - 如果您看到的只是 5a,那么它在 freq 上的 fscanf 中出现故障。 您可能为 freq 分配的空间太少,或者使用了不正确的类型。

What's freq defined as? What's prob defined as?

Is the first for loop meant to only run fscanf for freq? Or is it supposed to encompass the entire block?

EDIT: Rationale - If all you see is 5a, then it's faulting in the fscanf on freq. You may have allocated too little space for freq, or used an incorrect type.

远山浅 2024-08-05 02:58:47
  1. 检查返回代码,并在需要时使用 perror() 打印文件的错误代码(请参阅丹尼尔的回答)。 使用 FILE * 为 NULL 调用 fscanf() 肯定会出现段错误。

  2. 正如您指定的 %lf,freq 必须是至少包含 20 个双精度数的数组,而不仅仅是浮点数。
    请参阅 man ffrintf()。

  3. 使用调试器代替 printf()。

  1. Check the return code and print if needed the error code of your files with perror() (see. daniel answer). Calling fscanf() with a FILE * to NULL, will surely do a segfault.

  2. As you specified %lf, freq must be an array of at least 20 doubles, not just float.
    See man ffrintf().

  3. use a debugger instead of printf().

三生一梦 2024-08-05 02:58:47

您应该发布正在打开的任何文件的开头内容,因为它在 fscanf 中崩溃了。 该文件要么不存在,要么在与您尝试解析它的方式堆叠时格式错误。

You should post the contents of the beginning of whatever file is getting opened since it's crashing in fscanf. Either the file doesn't exist, or it's malformed when stacked against to how you are trying to parse it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文