2012-04-26 31 views
0

error: cast from 'void*' to 'unsigned int' loses precisionキャストエラーや無効な変換エラー

error: invalid conversion from 'unsigned int' to 'unsigned int**'

uは、私はこの行のエラーを取得しています、これを適切にキャストする方法を教えてくださいすることができます。

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

unsigned int width; 
unsigned int height; 
unsigned int **color = NULL; 

bool file_write() 
{ 
    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 true; 
} 
int method(int x, int y, 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; 
} 


void method1(double min_re, double max_re, double min_im, double max_im, int max_iterations) 
{ 
    for(int x = 0; x < width; x++) 
    { 
      for(int y = 0; y < height; ++y) 
      { 
        int m1 = method(x,y,min_re,max_re,min_im,max_im,max_iterations); 
        if(m1) 
        { 
          color[x][y] = m1*50; 
        } 
      } 
    } 
} 
int main(int argc, char *argv[]) 
{ 
    unsigned int max_iterations; 
    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); 
method1(min_re, max_re, min_im, max_im, max_iterations); 
time(&te); 
double diff = difftime(te,ts); 
file_write(); 
printf("Total Time elapsed: %f\n",diff); 
return 0; 
} 
+1

...大丈夫です! – DumbCoder

答えて

2
color = (unsigned int**)malloc(height*sizeof(unsigned int*)); 

は、それが、このことではないでしょうか?

+2

mallocの戻り値をCにキャストしないでください。意味がなく、実際には ''を含むことを忘れたという事実を隠すことができます。 CはC++ではありません。 'void *'は暗黙的にC言語のポインタ型に変換することができます。 –

+0

申し訳ありませんが、私は答えたときに質問にC++を付けました。 –

+0

ああ、十分に...あなたはおそらく、 'malloc'の使用がおそらくC++にとって間違っていると指摘したはずです。 –

3

なぜmallocの戻り値をunsigned intにキャストしていますか?

最初に、mallocの戻り値をCにキャストしないでください。それは無意味なので、含めるのを忘れたという事実を実際に隠すことができます。 CはC++ではありません。ボイド*が暗黙のうちに第二C.

内の任意のポインタ型に変換することができ、mallocはポインタを返し、あなたはunsigned int**としてcolorを定義している...まだあなたがそれにunsigned intなどunsigned int*を割り当てよう。明らかにそれらは互換性がありません。キャストを落とし、型を正しく使用/宣言してください。

+0

しかし、私は変数 'カラー' 2次元配列にメモリ空間を割り当てようとしています... –

+0

実際には色変数はプログラムに割り当てられたメモリに割り当てられています... –

+1

@visanio_learner:関連性があります。あなたは 'sunsigned int **'として色を宣言しました。ですから、正しい割り当ては 'color = malloc(some_size)'でしょう。キャストは必要なく、あなたが持っているキャストの1つ(unsigned int)は一般的に間違っています。 –

0

ポインタの配列を動的に割り当てようとしています。だから、何をする必要がある次のとおりです。

color = (unsigned int**)malloc(height*sizeof(unsigned int)); 

それの残りの部分は、これはC++のコードが、Cではありません