为什么我的程序停止响应? [英] Why does my programe stop responding?

查看:70
本文介绍了为什么我的程序停止响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是C的新手,我一直在做一个项目来寻找一段时间的功能。

我第一次尝试使用动态数组,因为我正在处理大量的数据点,我怀疑存在内存问题或与使用此数组相关的问题导致我的问题。



非常感谢任何建议我已经试图找出问题已经很多天了!

Hi guys,

I am new to C and I have been doing a project to find a period of a function.
I tried using dynamic arrays for the first time because I am dealing huge amounts of data points and I suspect that there is a problem with memory or something associated with the use of this arrays that is causing my problems.

Would appreciate any advise I have been trying to figure out what is wrong for many days already!

#include "stdafx.h"
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /*in order to use the memset function*/

int main()
{int channel,  maxdatapoints,size1=0,i=0,k=0,j=0,totalsize=0;
double time,*a,*b,*diff,*temp,difference;
int ans,f, maxf=0,maxf2=0,maxans2=0,maxans=0,maxans3=0,maxf3=0;

a=(double *)malloc(sizeof(double));
b=(double *)malloc(sizeof(double));
diff=(double *)malloc(sizeof(double));
     /*note to user, the (int *) or (double *) part is required in c++ but is not needed in c*/

    printf("Enter the maximum number of data points to compute \n");
    scanf("%d", &maxdatapoints);

FILE *fread, *histogramw, *histogramr,*result;


        if ((fread = fopen("raw.txt", "r")) == NULL)
        {
            printf("Cannot open %s\n", "raw.txt");
            exit(EXIT_FAILURE);
        }


            while (!feof(fread) && i<maxdatapoints)
            { /*Read the output file*/
                fscanf(fread, "%lf %*c %d\n", &time, &channel);

                /*If channel 1, write that to channel1.txt*/
                if(channel==1)
                {
                    a[i]=time;
                    b[i]=time;
                    size1=size1+1;
                    i=i+1;

                    temp=(double *)realloc(a,(i+2)*sizeof(double)); /*give the pointer some memory*/ /*note to user, the (int *) or (double *) part is required in c++ but is not needed in c*/

                        if (temp!=NULL)
                        {
                            a=temp;
                        }
                        else
                        {
                            free(a);
                            printf("Error allocating memory!\n");
                            return 1;
                        }

                        temp=(double *)realloc(b,(i+2)*sizeof(double)); /*give the pointer some memory*/ /*note to user, the (int *) or (double *) part is required in c++ but is not needed in c*/

                        if (temp!=NULL)
                        {
                            b=temp;
                        }
                        else
                        {
                            free(b);
                            printf("Error allocating memory!\n");
                            return 1;
                        }






                }
            }

    fclose(fread);

            for(i=0;i<size1;i++)

            { for(j=i+1;j<size1;j++)

            {
                diff[i]=abs(a[i]-b[j]);

            totalsize=totalsize+1;}
            }
            free(a);
    free(b);
        printf("Creating histogram\n");

        if ((histogramw = fopen("histogram.txt", "w")) == NULL)
        {
            printf("Cannot open %s\n", "histogram.txt");
            exit(EXIT_FAILURE);
        }


        double x, frequency=0; /*initialise the frequency of a variable, and the variable x*/

        for(i=0;i<totalsize;i++)
        {
            x=diff[i];
            for(j=0;j<totalsize;j++)
            { if (x==diff[j])
            {frequency=frequency+1;}
            }

                if(frequency>1) /*only print to file if frequency greater than 1, to reduce computing time*/
                {
                    fprintf(histogramw, "%lf %lf\n", x, frequency);

                    temp=(double *)realloc(diff,(j+2)*sizeof(double)); /*give the pointer some memory*/ /*note to user, the (int *) or (double *) part is required in c++ but is not needed in c*/

                        if (temp!=NULL)
                        {
                            diff=temp;
                        }
                        else
                        {
                            free(diff);
                            printf("Error allocating memory!\n");
                            return 1;
                        }

                }

                frequency=0;
            }

        fclose(histogramw);
        free(diff);
        /*Now we want to find the most and second most frequent variable*/

        if ((histogramr = fopen("histogram.txt", "r")) == NULL)
        {
            printf("Cannot open %s\n", "histogram.txt");
            exit(EXIT_FAILURE);
        }




        /*First we find the most frequent variable*/

        printf("Finding the 1st to 3rd highest period\n");

        while(!feof(histogramr))
        {
            fscanf(histogramr, "%lf %lf\n", &ans, &f);
            if(f>maxf)
            {
                maxf=f;
                maxans=ans;
            }
        }

        rewind(histogramr);

        /*Next we find the second most frequent variable*/

        while(!feof(histogramr))
        {
            fscanf(histogramr, "%lf %lf\n", &ans, &f);
            if((f>maxf2)&&(ans!=maxans))
            {
                maxf2=f;
                maxans2=ans;
            }
        }

    rewind(histogramr);

        while(!feof(histogramr))
        {
            fscanf(histogramr, "%lf %lf\n", &ans, &f);
            if((f>maxf3)&&(ans!=maxans)&&(ans!=maxans2))
            {
                maxf3=f;
                maxans3=ans;
            }
        }

            printf("Cannot open 4\n");

        fclose(histogramr);

        printf("The highest period is %lf with value of %lf\nThe second highest period is %lf with value of %lf\n The third highest period is %lf with value of %lf\n ",maxf,maxans,maxf2,maxans2,maxf3,maxans3);


    /*if peak frequency is found, print the results to file*/
        if ((result = fopen("result.txt", "w")) == NULL)
        {
            printf("Cannot open %s\n", "result.txt");
            exit(EXIT_FAILURE);
        }

        fprintf(result, "The highest period is %lf with value of %lf\nThe second highest period is %lf with value of %lf\n The third highest period is %lf with value of %lf\n ",maxf,maxans,maxf2,maxans2,maxf3,maxans3);

    printf("Cannot open 5\n");

    /*Free the pointer*/



    return 0;

    }

推荐答案

首先,此部分称为快速答案,不仅表示预期的时间跨度得到答案,或答案的长度,以及问题的复杂性和长度。发布200多行代码几乎没有关于从哪里开始查看的额外信息在这方面没有帮助!



其次,更具体一点。 没有回应还不够清楚。您为程序提供的数据是什么,您是如何调用它的?您期望(下一个)有什么样的回复,以及您从程序中获得的最后处理标志是什么?它甚至开始运行了吗?你尝试过不同的输入文件吗?您是否尝试过一个非常小的输入文件?



三,学会使用调试器:如果程序没有响应,请在调试器中启动它,然后在一段时间后点击中断或暂停,并调查你的调用堆栈。在那里,你可以看到计算机所处的程序的哪一部分。显然,这个简单的程序会立即回答你的问题,省去你在这里发布的努力。



请记住,我们没有看到或猜测您的输入,也不知道您的期望,也无法像计算机一样快速地远程分析您的程序流程。您的调试器可以帮助您更快地回答有关意外程序行为的大多数问题,而不是在任何论坛上发布问题 - 您只需要学会使用它!
First this section is called quick answers, not only to indicate the expected time span to receive an answer, or the length of an answer, but also the complexity and length of the question. Posting 200+ lines of code with virtually no additional information as to where to start looking doesn't help in that respect!

Second, be more specific. "doesn't respond" is not sufficiently clear. What are the data you fed to your program, how did you invoke it. What kind of response did you expect (next), and what is the last sign of processing you got from your program, if any? Did it even start running? Did you try a different input file? Did you try with a trivial, very small input file?

Third, learn to use a debugger: if a program isn't responding, start it in the debugger, then just hit "break" or "pause" after some time, and investigate your call stack. There you can see what part of the program the computer is stuck in. Obviously, this simple procvess would immediately answer your question, saving you the effort to post it here.

Remember we don't see or can guess your input, nor do we know what you expect, nor can we remotely analyze your program flow as fast as your computer can. Your debugger can help you answer most questions regarding unexpected program behaviour much faster than you can post the problem on any forum - you just have to learn to use it!


您正在存储 size1 数值到 diff 数组,而数组大小没有增加:

You are storing size1 values to the diff array while the array size has not been increased:
for(i=0;i<size1;i++)
{ 
    for(j=i+1;j<size1;j++)
    {
        diff[i]=abs(a[i]-b[j]);
        totalsize=totalsize+1;
    }
}



我还认为你想用 totalsize 作为索引 diff 这里。



我建议使用max预先分配数组。大小( maxdatasize a b 并计算 totalsize for diff )。然后你不必重新分配。


I also think that you want to use totalsize as index for diff here.

I suggest to pre-allocate the arrays using the max. size (maxdatasize for a and b and calculated totalsize for diff). Then you don't have to reallocate.


这篇关于为什么我的程序停止响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆