English 中文(简体)
过失部分
原标题:segmentation fault

我正试图在C++的顺序规划中清晰地看到一个红树的图像,但我正在经历一段时期时陷入分裂。 我对错误没有想法,但我的方案完全没有错误。

#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
");
        fprintf(fractal,"# %s
", "Mandelbrot_imageSequential.ppm");
        fprintf(fractal,"%d %d
", height, width);
        fprintf(fractal,"40
");
        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.
");
        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	width = %d	maximum_iterations = %d	minimum_x-value = %.2f	maximum_x-value = %.2f	minimum_y-value = %.2f	maximum_y-value = %.2f	threshold_value = %.2f	no. of threads = %d	
",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
",diff);
    return 0;
}

如何纠正这种分割错误?

问题回答

至少有一个问题出现在档案中。

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

我认为,颜色应当是投入参数。

如果你坐在北纬线机器上,则采取下列行动:

$ulimit -c unlimited

之后,该法典将出台。 通知是核心。 fire

$gdb ./your_app core.[pid]

它将请你说明发生绑架事件的情况。 发出“后退”指令,促使人们看到电话等级。

编辑“-g”旗帜,以获得更多的饮料。

贵国法典有两个重大问题:

  1. 页: 1

    您需要通过第一个<代码>color,作为file_write(:

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

    And declare the other color as an argument to file_write():

    int file_write(unsigned int **color, unsigned int width, unsigned int height)
    {
        /* unsigned int **color = NULL; // Removed */
       ...
    
  2. 页: 1 你们需要把它称作 lo。 类似于:

    /* 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);
        }
    }
    

Then, of course, you should check the return values of malloc(), fopen(), fprintf(), fclose(), ... , and check that the input variables have reasonable values and so on.

我还注意到,您通过<代码>width和h 8, ,以不同顺序通过file_write(method()。 为避免未来的头痛,我将将<条码>(方法)功能改为<条码>(方法(x, y, width,高),以便横向和纵向论点按同一顺序通过。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签