Consider:
int main()
{
const char *p = new const char[10]();
delete[] p;
}
No problems here.
Now consider:
int main()
{
const char *p = static_cast<const char *>(calloc(10, 1));
free(p);
}
Error:
prog.cpp: In function 'int main()':
prog.cpp:6:8: error: invalid conversion from 'const void*' to 'void*' [-fpermissive]
free(p);
Any reason you can't do this?
In practice, you might allocate non-const but when you want to delete it, you may have only a const char *
pointer left to do it with. delete[]
lets you do it, but not free
. Is there a reason for this?