2012-04-25 6 views
-2

나는 C++의 순차 프로그래밍으로 mandelbrot 이미지를 명확하게 표시하려고하지만 런타임 중에 세그먼트 오류가 발생합니다. 나는 seg에 대해 전혀 모른다. 오류가 있지만 내 프로그램이 오류없이 완벽하게 컴파일됩니다.분할 오류

#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; 
} 

이 세분화 오류를 수정하는 방법은 무엇입니까?

+1

디버거가 segfault 지점을 찾도록하십시오. – Anthales

+0

좋습니다. 시도해 보겠습니다. –

+3

나는 당신의'file_write'에 하나의 문제점을 발견 할 수 있습니다 :'unsigned int ** color'를위한 메모리를 절대로 할당하지 말고, 당신의 main에서'color'를 함수로 넘겨주지 않았습니다. – Anthales

답변

2

적어도 하나의 문제가 file_write 함수에 있습니다. R = (color[y][x]*10);

unsigned int **color = NULL;

    1. 는 I 색은 입력 변수되어야 가정한다.

  • +0

    이 seg fault에 대한 해결책이 무엇인지 알 수 있습니까? –

    +0

    예. 1. int file_write (부호없는 int 너비, 부호없는 int 높이, 부호없는 정수 ** 색상) {...}', 2.'file_write (width, height, color);'. 그러나 또 다른 문제가 있습니다. 색상은 초기화되지 않고 어디에서나 사용되지 않는 기본 색상으로 할당됩니다. 즉, 'file_write'함수는 무언가를 저장합니다. – megabyte1024

    +0

    이제 C++을 실행하는 대신 C++에서 실행하려고합니다. 오류 및 메모가 표시됩니다. > 'for 루프'초기 선언은 C99 모드에서만 허용됩니다. 참고 : -std = c99 또는 -std = gnu99 코드를 컴파일하려면 –

    1

    리눅스 머신에있는 경우 다음을 수행하십시오

    $ulimit -c unlimited 
    

    그런 다음 코드를 실행합니다. 코어. [pid] 파일이 생성됩니다. gdb를 다음과 같이 실행 시키십시오.

    $gdb ./your_app core.[pid] 
    

    segfault가 발생한 부분이 표시됩니다. 호출 계층 구조를 보려면 gdb 프롬프트에서 "backtrace"명령을 실행하십시오.

    더 자세한 gdb 출력을 얻으려면 "-g"플래그로 컴파일하십시오.

    +0

    나는 gdb를 실행하려고했지만 아무런 그런 말은하지 않는다. 파일 또는 디렉토리 ... –

    +0

    gdb에 제공 한 인수는 무엇입니까? 바이너리가있는 디렉토리에 있습니까? "핵심"파일의 존재를 확인 했습니까? – Aftnix

    +0

    예. 내 디렉토리에 있습니다 ... –

    0

    코드에 두 가지 문제가있다는 :

    1. 당신은 color 배열에 대한 메모리를 할당하지만 다음 다른colorNULL로 초기화된다 file_write() 내부를 사용합니다. file_write()에 인수로 다른 color

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

      을 그리고 선언 :

      당신은 file_write()에 인수로 첫 color을 통과해야 한 번만 method()를 호출하고

      int file_write(unsigned int **color, unsigned int width, unsigned int height) 
      { 
          /* unsigned int **color = NULL; // Removed */ 
          ... 
      
    2. 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(), ..., 입력 변수 등 합리적인 값이 있는지 확인 .

    widthheightfile_write()method()과 다른 순서로 전달하는 것으로 나타났습니다. 장래의 두통을 피하기 위해, method() 기능을 method(x, y, width, height)으로 변경하여 수평 및 수직 인수가 동일한 순서로 전달되도록합니다.