我应该在Visual Studio中使用void **返回吗 [英] Should I cast void** return in Visual Studio

查看:79
本文介绍了我应该在Visual Studio中使用void **返回吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 9.0(2008)编译了一些代码.

I compile some code with visual studio 9.0 (2008).

其行为符合预期,但是当我用一些手工分配一些2D数组时功能,Visual Studio会生成一些C4133警告:

The behave as expected, but when I allocate some 2D array with some hand made functions, Visual-Studio generates some C4133 warning:

void ** alloc_2d(int w, int h, size_t type_size);
void free_d2(void ** mem);

int main (void)
{        
    float ** data;

    /* Here is generated a C4133 warning:
       "incompatible type from void ** to float **" */
    data = alloc_2d(100, 100, sizeof **data);

    /* do things with data */

    /* free data */
    free_2d(data);

    return 0;
}

我理解为什么会生成此警告,但是我想知道应该怎么做才能使其变得安静.

I understand why this warning is generated, but I wonder what I should do to make it quiet.

我该怎么办?

  • 忍受警告吗?
  • 禁用警告(我认为这很危险)?
  • alloc_2d 调用周围禁用警告(带有一些特定于视觉工作室)?
  • 强制转换了函数的返回值(但是[我是否强制转换malloc的结果?)
  • Live with the warning?
  • Disable the warning (dangerous I think)?
  • Disable the warning around the alloc_2d calls (with some macros specific to visual studio)?
  • cast the function return (But [Do I cast the result of malloc?)

第二个问题:

  • 新手/其他编译器是否意识到这种类型的转换?

void ** 后面隐藏着两个数组:一个大数组用于存储我需要连续的所有数据,另一个大数组用于浏览不同的行.

Behind the void** are hidden two arrays: one big to store all data I need to be contiguous, and one other to browse through differents lines.

实现看起来像(我删除了错误检查)

The implementation looks like (I removed the error checking)

void **alloc_2D_array(int w, int h, size_t size)
{
    void ** mem = malloc(w * sizeof *mem);
    *mem = malloc(w*h*size);
    for (i = 1; i < w; ++i)
    {
        mem[i] = (void*)((char*)mem[0] + i*w*size);
    }
    return mem;
}

推荐答案

C中的通用指针类型为 void * .那不是不是意味着 void ** 也是通用指针类型,该规则不适用于递归.

The generic pointer type in C is void*. That does not mean that void** is also a generic pointer type, the rule does not apply recursively.

相反,从 void ** float ** 的转换或另一种方法是无效的指针转换.它们不是兼容类型-编译器必须发出诊断消息.忽略警告可能会导致数据未对齐或严格的别名问题(无论哪种情况都存在错误).

Rather, a conversion from void** to float** or the other way around is an invalid pointer conversion. They are not compatible types - the compiler must issue a diagnostic message. Ignoring the warning might result in misaligned data or strict aliasing issues - in either case bugs.

我该怎么办?

修复代码,使其不包含禁止的指针转换.

Fix the code so that it doesn't contain forbidden pointer conversions.

新手/其他编译器是否意识到这种类型的转换?

Are newer/other compilers aware of this kind of cast?

自C的首次标准化以来,此特定规则并未更改.

This particular rule has not changed since the first standardization of C.

关于应如何修复代码……不应该.您应该从头开始重写它,以便它分配 arrays 而不是基于指针的查找表".没有明显的理由使您从这里的这种查找表中受益,但是有许多原因使您避免使用它.

As for how you should fix your code... you shouldn't. You should rewrite it from scratch so that it allocates arrays and not "pointer-based look-up tables". There is no obvious reason why you would benefit from such a look-up-table here, but many reasons why you should avoid it.

此外,如果您使用指向VLA的指针,您的代码将更具可读性.

In addition, your code would be much more readable if you use pointers to VLA.

有关如何正确执行此操作的示例,请参见正确分配多维数组(代码示例位于答案的最底部).

See Correctly allocating multi-dimensional arrays for examples of how to do this proper (code example at the very bottom of the answer).

这篇关于我应该在Visual Studio中使用void **返回吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆