【问题标题】:cast error and invalid conversion error转换错误和无效转换错误
【发布时间】:2012-05-07 14:31:30
【问题描述】:

错误:从“void*”转换为“unsigned int”会丢失精度

错误:从 'unsigned int' 到 'unsigned int**' 的无效转换

你能告诉我如何正确地转换这个吗,我在这一行遇到错误:

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

在主函数内部。

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

【问题讨论】:

  • 这不是 C++ 代码,而是 C !!

标签: c


【解决方案1】:

为什么要将 malloc 的返回值转换为 unsigned int

首先,不要在 C 中强制转换 malloc 的返回值。这是没有意义的,实际上可以隐藏您忘记包含的事实。在这方面,C 不是 C++。 void* 可以隐式转换为 C 中的任何指针类型。

其次,malloc 返回一个指针,并且您已将 color 定义为 unsigned int**... 但是您尝试将 unsigned intunsigned int* 分配给它。显然这些是不相容的。只需删除强制转换并正确使用/声明类型即可。

【讨论】:

  • 其实这里它的颜色变量是在分配给程序的内存中分配的...
  • @visanio_learner:我不明白这有什么关系。您将颜色声明为sunsigned int**。太好了,所以正确的分配是color = malloc(some_size)。不需要强制转换,您拥有的强制转换之一(无符号整数)通常是错误的。
  • 朋友,我可以解释一下,color = (unsigned int**)malloc(heightsizeof(unsigned int));颜色是 ** 它是一个指针。所以我们正在做的是 malloc(some_size) 在分配内存空间的指针中保存一个值,然后 unsigned int** 将保存该值的起始地址。
  • @visanio_learner:谢谢,但我了解 malloc 是如何完美运行的。我对此并不感到困惑,我很困惑为什么你会转换结果(因为它在 C 中是不需要的,实际上可能是一件坏事)以及为什么你会转换为不兼容的类型。
  • 但它对我有用,我得到了输出......我真的不明白你对这个“不兼容”的看法
【解决方案2】:
color = (unsigned int**)malloc(height*sizeof(unsigned int*));

不应该是这样吗?

【讨论】:

  • 请不要将 malloc 的返回值转换成 C 语言。这毫无意义,实际上可以隐藏您忘记包含 &lt;stdlib.h&gt; 的事实。在这方面,C 不是 C++。 void* 可以隐式转换为 C 中的任何指针类型。
  • 对不起,当我回答时,问题被标记为 C++。
  • 啊,很公平...尽管您可能应该注意到 malloc 的使用对于 C++ 可能是错误的。
【解决方案3】:

您正在尝试动态分配指针数组。所以你需要做的是:

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

其他都还好……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 2013-11-15
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多