I compile some code with visual studio 9.0 (2008).
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.
What should I do?
- 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?)
Second question:
- Are newer/other compilers aware of this kind of cast?
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;
}