-2

我正在尝试使用 C++ 中的顺序编程清楚地获得 mandelbrot 图像,但在运行时出现分段错误。我对赛格一无所知。错误,但我的程序完美编译,没有错误。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int file_write(unsigned int width, unsigned int height)
{
    unsigned int **color = NULL;
    FILE *fractal = fopen("mandelbrot_imageSequential.ppm","w+");
    if(fractal != NULL)
    {
        fprintf(fractal,"P6\n");
        fprintf(fractal,"# %s\n", "Mandelbrot_imageSequential.ppm");
        fprintf(fractal,"%d %d\n", height, width);
        fprintf(fractal,"40\n");
        int x = 0, y = 0;
        unsigned int R = 0, G = 0, B = 0;
        for(x = 0; x < width; ++x)
        {
            for(y = 0; y < height; ++y)
            {
                R = (color[y][x]*10);
                G = 255-((color[y][x]*10));
                B = ((color[y][x]*10)-150);
                if(R == 10)
                    R = 11;
                if(G == 10)
                    G = 11;
                if(B == 10)
                    B = 11;
                putc(R, fractal);
                putc(G, fractal);
                putc(B, fractal);
            }
        }
        fclose(fractal);
    }
    return 0;
}
int method(int x, int y, int height, int width, double min_re, double max_re, double min_im, double max_im, int max_iterations)
{
    double threshold = 4;
    double x_factor = (max_re-min_re)/(width-1);
    double y_factor = (max_im-min_im)/(height-1);
    double c_im = max_im - y*y_factor;
    double c_re = min_re + x*x_factor;
    double Z_re = c_re, Z_im = c_im;
    unsigned int col = 0;
    for(unsigned n = 0; n < max_iterations; ++n)
    {
        double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
        if(Z_re2 + Z_im2 > threshold)
        {
            col = n;
            break;
        }
        Z_im = 2 * Z_re * Z_im + c_im;
        Z_re = Z_re2 - Z_im2 + c_re;
    }
    return col;
}
int main(int argc, char *argv[])
{
    unsigned int width;
    unsigned int height;
    unsigned int max_iterations;
    unsigned int **color = NULL;
    int x,y;
    double threshold;
    double min_re;
    double max_re;
    double min_im;
    double max_im;
    unsigned int NUM_OF_THREADS;
    if(argc != 10)
    {
        printf("There is an error in the input given.\n");
        return 0;
    }
    else
    {
        height = atoi(argv[1]);
        width = atoi(argv[2]);
        max_iterations = atoi(argv[3]);
        min_re = atof(argv[4]);
        max_re = atof(argv[5]);
        min_im = atof(argv[6]);
        max_im = atof(argv[7]);
        threshold = atoi(argv[8]);
        NUM_OF_THREADS = atoi(argv[9]);
    }
    color = (unsigned int**)malloc(height*sizeof(unsigned int*));
    printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tthreshold_value = %.2f\tno. of threads = %d\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,threshold,NUM_OF_THREADS);
    for(x = 0; x < height; x++)
    {
        color[x] = (unsigned int*)malloc(width*sizeof(unsigned int));
    }
    time_t ts,te;
    time(&ts);
    method(x,y,height,width,min_re,max_re,min_im,max_im,max_iterations);
    time(&te);
    double diff = difftime(te,ts);
    file_write(width, height);
    printf("Total Time elapsed: %f\n",diff);
    return 0;
}

如何纠正这种分段错误?

4

3 回答 3

2

至少有一个问题出在 file_write 函数中。

  1. unsigned int **color = NULL;
  2. R = (color[y][x]*10);

我假设颜色应该是一个输入参数。

于 2012-04-25T10:54:49.577 回答
1

如果您在 Linux 机器上,请执行以下操作:

$ulimit -c unlimited

然后运行代码。注意生成了一个 core.[pid] 文件。像下面这样启动 gdb

$gdb ./your_app core.[pid]

它将带您到发生段错误的语句。在 gdb 提示符下发出“回溯”命令以查看调用层次结构。

请记住使用“-g”标志编译以获得更详细的 gdb 输出。

于 2012-04-25T11:06:25.423 回答
0

您的代码有两个主要问题:

  1. 您为数组分配内存,color然后使用不同 color的内部file_write()初始化为NULL.

    您需要将第一个color作为参数传递给file_write()

    int main(...)
    {
        ...
        file_write(color, width, height);
        printf("Total Time elapsed: %f\n",diff);
        return 0;
    }
    

    并声明另一个color作为参数file_write()

    int file_write(unsigned int **color, unsigned int width, unsigned int height)
    {
        /* unsigned int **color = NULL; // Removed */
       ...
    
  2. 你只调用method()一次,没有将任何东西存储到color. 您需要循环调用它。类似于:

    /* Untested */
    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            color[y][x] = method(x,y,height,width,min_re,max_re,min_im,max_im,max_iterations);
        }
    }
    

malloc()然后,当然,您应该检查, fopen(), fprintf(), , ...的返回值fclose(),并检查输入变量是否具有合理的值等等。

我还注意到您以不同的顺序传递到widthand 。为了避免以后的麻烦,我会将函数更改为,以便水平和垂直参数以相同的顺序传递。heightfile_write()method()method()method(x, y, width, height)

于 2017-01-16T05:54:29.970 回答