A.4 Conversion to void *
Note
In C, there is a standard conversion from void * to T * (for any object type T). In C++, there is no such conversion. Such conversions require a cast in C++.
The following Standard C library functions return void *:
calloc, malloc, realloc, bsearch, memcpy, memmove, memchr, memset
C++ requires an explicit cast when assigning the return value of such a function to a pointer to non-void type.
Example
int* p; p = malloc(10 * sizeof(int));
In C++, the assignment to p requires an explicit cast, as in
p = (int *)malloc(10 * sizeof(int));
Guideline
In C++, use operator new instead of calloc, malloc, or realloc. (See item A.12).
Ignore the return value of a memcpy, memmove, and memset. (They just return their first argument converted to void *.)
For all other functions that return void * (standard or user-defined), use an explicit cast when converting the return value to another pointer type.
|